fork download
  1. $xx={}
  2. $yy=[]
  3. $num=0
  4.  
  5. class Patterns
  6. @a = []
  7. def initialize(x)
  8. @a = convert(x)
  9. end
  10.  
  11.  
  12. def convert(x)
  13. ret = []
  14. maxnum = x.max { |a, b| a.length <=> b.length}.length
  15. z = x[0..-1]
  16. for l in 1..(maxnum) do
  17. y = x[0..-1]
  18. while y.empty? == false do
  19. bb = y[0]
  20. y = y.reject { |m| m==bb }
  21. if (bb.length < l) then
  22. next
  23. end
  24. aa=Array.new(l,1)
  25. if(l<bb.length+1) then
  26. aa=aa.concat(Array.new(bb.length-l,0))
  27. end
  28.  
  29. s=check(x,aa,bb)
  30.  
  31. if s.length < 3 then
  32. next
  33. end
  34. if (s.min..s.max).to_a.all?{ |j| s.include?(j)} then
  35. pat = Pattern.new(aa,bb,s.min,s.max)
  36. ret << pat
  37. for xx in s.min..s.max do
  38. k=[]
  39. for n in 0..aa.length-1 do
  40. k[n] = aa[n]*xx+bb[n]
  41. end
  42. y.delete(k)
  43. z.delete(k)
  44. end
  45. end
  46. end
  47. end
  48. for k in 0..z.length-1 do
  49. aa = Array.new(z[k].length,0)
  50. bb = z[k]
  51. pat = Pattern.new(aa,bb,0,0)
  52. ret << pat
  53. end
  54.  
  55. return ret
  56. end
  57.  
  58. def check(y,a,b)
  59. ret = []
  60. if a.length != b.length then
  61. return ret
  62. end
  63.  
  64. for i in 0..(y.length-1) do
  65. $num+=1
  66. x=(y[i][0]-b[0])/a[0]
  67. if y[i].length != a.length then
  68. next
  69. end
  70.  
  71. if (0..a.length-1).to_a.all?{ |j| y[i][j]==a[j]*x+b[j] } then
  72. ret << x
  73. end
  74. end
  75. return ret
  76. end
  77.  
  78. def to_s
  79. s = "["
  80. for i in 0..(@a.length-1) do
  81. s += @a[i].to_s + ","
  82. end
  83. s = s.chop
  84. s += "]"
  85. return s
  86. end
  87. end
  88.  
  89.  
  90.  
  91. class Pattern
  92. @a = []
  93. @b = []
  94. @first = 0
  95. @last = 0
  96. def initialize(a,b,first,last)
  97. @a = a[0..-1]
  98. @b = b[0..-1]
  99. @first = first
  100. @last = last
  101. end
  102.  
  103. def to_s
  104. s = "{["
  105. for i in 0..(@a.length-1) do
  106. s += @a[i].to_s + "x+" + @b[i].to_s + ","
  107. end
  108. s=s.chop
  109. s += "]:"
  110. s += @first.to_s + "<=x<=" + @last.to_s + "}"
  111. if @first == @last then
  112. s = @b.to_s
  113. end
  114. return s
  115. end
  116. end
  117.  
  118.  
  119. def lose(a)
  120.  
  121. if a.length == 1 && a[0] == 1 then
  122. if $xx[[1]] == nil then
  123. $xx[[1]] = true
  124. $yy << [1]
  125. end
  126.  
  127. return true
  128. end
  129.  
  130. if $xx[a] == true then
  131. return $yy.include?(a)
  132. end
  133.  
  134. ret = true
  135. for i in 0..(a.length-1) do
  136. for j in 1..a[i] do
  137. if i==0 && j==1 then
  138. next
  139. end
  140.  
  141. $num+=1
  142. b=a[0..-1]
  143. for k in i..(a.length-1) do
  144. $num+=1
  145. if a[k] >= j then
  146. b[k] = j -1
  147. end
  148. end
  149. if j==1 then
  150. b=a[0..i-1]
  151. end
  152.  
  153. if ($xx[b] == nil) then
  154. if lose(b) then
  155. ret = false
  156. end
  157. else
  158. if $yy.include?(b) then
  159. ret = false
  160. end
  161. end
  162. end
  163. end
  164.  
  165. if ret == true then
  166. $yy << a
  167. end
  168. $xx[a] = true
  169.  
  170. return ret
  171. end
  172.  
  173. m=4
  174. n=16
  175. #x = Array.new(m,n)
  176. x=[24,24,24]
  177. print "num="
  178. p $num
  179. lose(x)
  180. print "num="
  181. p $num
  182.  
  183. p $xx.length
  184. p $yy.length
  185. for i in 1..x.length do
  186. tt = $yy.select { |n| n.length == i }
  187. print "count="
  188. print tt.length
  189. print " "
  190. p tt
  191. end
  192.  
  193. set = Patterns.new($yy)
  194.  
  195. print "p="
  196. p $num
  197.  
  198. puts set.to_s
  199. print "p="
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
Success #stdin #stdout 2.82s 8452KB
stdin
Standard input is empty
stdout
num=0
num=331552
2924
129
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=105 [[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, 6, 5], [9, 7, 2], [9, 9, 6], [10, 5, 5], [10, 6, 4], [10, 8, 2], [11, 6, 6], [11, 7, 5], [11, 9, 2], [12, 7, 6], [12, 8, 5], [12, 9, 7], [12, 10, 2], [12, 12, 8], [13, 7, 7], [13, 8, 6], [13, 9, 5], [13, 11, 2], [14, 8, 7], [14, 9, 8], [14, 10, 5], [14, 11, 9], [14, 12, 2], [14, 14, 10], [15, 8, 8], [15, 10, 7], [15, 11, 5], [15, 13, 2], [16, 9, 9], [16, 10, 8], [16, 11, 7], [16, 12, 5], [16, 14, 2], [17, 10, 9], [17, 11, 8], [17, 12, 7], [17, 13, 5], [17, 14, 11], [17, 15, 2], [17, 17, 12], [18, 10, 10], [18, 12, 9], [18, 13, 7], [18, 14, 5], [18, 16, 2], [19, 11, 10], [19, 12, 11], [19, 13, 9], [19, 14, 7], [19, 15, 5], [19, 16, 12], [19, 17, 2], [19, 19, 13], [20, 11, 11], [20, 12, 10], [20, 14, 9], [20, 15, 7], [20, 16, 5], [20, 18, 2], [21, 12, 12], [21, 13, 10], [21, 15, 9], [21, 16, 7], [21, 17, 5], [21, 18, 11], [21, 19, 2], [22, 13, 11], [22, 14, 12], [22, 15, 13], [22, 16, 9], [22, 17, 7], [22, 18, 5], [22, 19, 14], [22, 20, 2], [22, 22, 15], [23, 13, 12], [23, 14, 13], [23, 15, 11], [23, 16, 14], [23, 17, 9], [23, 18, 7], [23, 19, 5], [23, 20, 15], [23, 21, 2], [23, 23, 16], [24, 13, 13], [24, 15, 12], [24, 16, 11], [24, 18, 9], [24, 19, 7], [24, 20, 5], [24, 22, 2]]
p=368317
[{[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},{[1x+11,1x+6,0x+6]:0<=x<=2},{[1x+11,1x+7,0x+5]:0<=x<=13},{[1x+15,1x+10,0x+7]:0<=x<=9},{[1x+18,1x+10,0x+10]:0<=x<=3},{[1x+18,1x+12,0x+9]:0<=x<=6},[1],[2, 2, 1],[3, 1, 1],[5, 5, 3],[6, 3, 3],[7, 4, 3],[7, 7, 4],[9, 6, 5],[9, 9, 6],[10, 5, 5],[12, 9, 7],[12, 12, 8],[13, 7, 7],[14, 8, 7],[14, 9, 8],[14, 11, 9],[14, 14, 10],[15, 8, 8],[16, 9, 9],[16, 10, 8],[17, 10, 9],[17, 11, 8],[17, 14, 11],[17, 17, 12],[19, 12, 11],[19, 16, 12],[19, 19, 13],[20, 11, 11],[21, 12, 12],[21, 18, 11],[22, 13, 11],[22, 14, 12],[22, 15, 13],[22, 19, 14],[22, 22, 15],[23, 13, 12],[23, 14, 13],[23, 15, 11],[23, 16, 14],[23, 20, 15],[23, 23, 16],[24, 13, 13],[24, 15, 12],[24, 16, 11]]
p=