0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

0
Posted at

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

def tree(ns,n,t)
  return 0 if n < t
  return 1 if n == t
  return ns[n] if ns[n] > 0
  nl = n/2-10
  nr = n*2/3
  ns[n] = tree(ns,nl,t) + tree(ns,nr,t)
end

def solve(q)
  root,target = q.split(',').map(&:to_i)
  ns = Hash.new(0)
  tree(ns,root,target)
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	123,4	5	
1	1,1	1	
2	2,1	1	
3	3,3	1	
4	19,5	1	
5	69,5	3	
6	88,9	2	
7	1,100	0	
8	100,4	4	
9	101,9	0	
10	456,7	7	
11	567,8	12	
12	756,10	10	
13	789,10	12	
14	896,29	2	
15	7764,6	664	
16	1234,56	3	
17	8563,29	35	
18	12345,67	10	
19	72927,51	263	
20	71441,145	22	
21	123456,78	397	
22	123456,789	1	
23	592741,216	55	
24	913826,584	81	
25	1234567,89	2293	
26	10000000,1	19383507	
27	12345678,9	3567354	
28	6215879,358	2907	
29	12345678,90	79419	
30	5745432,1032	1287	```
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?