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.include?([1]) == false then
  123. $xx << [1]
  124. $yy << [1]
  125. end
  126.  
  127. return true
  128. end
  129.  
  130. if $xx.include?(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. b=a[0..-1]
  142. for k in i..(a.length-1) do
  143. $num+=1
  144. if a[k] >= j then
  145. b[k] = j -1
  146. end
  147. end
  148. if j==1 then
  149. b=a[0..i-1]
  150. end
  151.  
  152. if ($xx.include?(b) == false) then
  153. if lose(b) then
  154. ret = false
  155. end
  156. else
  157. if $yy.include?(b) then
  158. ret = false
  159. end
  160. end
  161. end
  162. end
  163.  
  164. if ret == true then
  165. $yy << a
  166. end
  167. $xx << a
  168.  
  169. return ret
  170. end
  171.  
  172. m=3
  173. n=16
  174. #x = Array.new(m,n)
  175. x=[20,24,4]
  176. lose(x)
  177.  
  178. print "$num = "
  179. p $num
  180. p $xx.length
  181. p $yy.length
  182. for i in 1..m do
  183. tt = $yy.select { |n| n.length == i }
  184. print "count="
  185. print tt.length
  186. print " "
  187. p tt
  188. end
  189.  
  190. set = Patterns.new($yy)
  191. puts set.to_s
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
Success #stdin #stdout 4.85s 8364KB
stdin
Standard input is empty
stdout
$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]]