#ruby
#yhpg
#どうかく

オフラインリアルタイムどう書くE17の問題をRubyで解く

More than 1 year has passed since last update.

問題はこちら
http://nabetani.sakura.ne.jp/hena/orde17palin/

def gen_kaibun_num(n,b)
  d,m = n.divmod(2)
  return (0...b).map{|e| e.to_s(b)} if d == 0
  s_min = '1'+'0'*(d-1)
  s_max = (b-1).to_s(b)*d
  kns = []
  (s_min.to_i(b)..s_max.to_i(b)).each{|e|
    s = e.to_s(b)
    if m > 0
      (0...b).each {|c|
        kns << s + c.to_s(b) + s.reverse
      }
    else
      kns << s + s.reverse
    end
  }
  kns
end

def solve(q)
  x,y,b = q.split(',').map(&:to_i)
  c = 0
  xb = x.to_s(b)
  yb = (y-1).to_s(b)
  (xb.size..yb.size).each {|n|
    gen_kaibun_num(n,b).each{|e|
      kn = e.to_i(b)
      c += 1 if kn >=x && kn < y
    }
  }
  c
end

DATA.readlines.each do |line|
  no,q,a = line.strip.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans.to_s
  puts ans == a.to_i ? ' o' : ' x'
end
__END__
0   12,34,5 5
1   10,11,10    0
2   1,100,3 18
3   11,12,10    1
4   12,13,10    0
5   123,456,7   33
6   38,274,14   17
7   98,76543,2  535
8   987,6543,2  103
9   5057,5202,3 2
10  98,76543,21 589
11  987,6543,21 264
12  1097,2889,11    35
13  2764,6482,17    132
14  16333,24085,8   121
15  21759,67173,20  114
16  32026,57805,22  53
17  188318,407853,6 523
18  51669,116065,30 72
19  294104,515248,32    216
20  444257,740280,15    1316
21  645098,2741620,9    2876
22  12345,987654321,2   62684
23  2467130,8433468,2   2902
24  323901,4712975,10   4389
25  12345,987654321,36  67446
26  3969344,4086910,24  205
27  19743263,83912295,5 11553
28  6349529,39870823,10 6637
29  66160071,153732445,5    5605
30  18799557,189007582,14   33741
31  78547566,225312226,20   18346
32  143084571,506549072,18  62323
33  2099642384,2789567569,6 14787