#! ruby # yukicoder My Practice # author: Leonardone @ NEETSDKASU def gi(); gets.to_i; end def gis(); gets.chomp.split.map(&:to_i); end =begin 1~Nまでの間には aの倍数が n/a 個 bの倍数が n/b 個 cの倍数が n/c 個 a*bの倍数が n/(a*b)個 b*cの倍数が n/(b*c)個 c*aの倍数が n/(c*a)個 a*b*cの倍数が n/(a*b*c)個 和集合とかいうやつで集合の足し算引き算で求める感じ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 2 * * * * * * * * * * * * * * 3 * * * * * * * * * * 5 * * * * * * 6 @ @ @ @ @ 10 @ @ @ 15 @ @ 30 # 最小公倍数なケースがあるんよ 30 42 60 70 84 90 120 126 140 210 280 a = 6 * * * * * * * b = 10 * * * * * * * * c = 14 * * * * * * * ab= 60 @ @ bc= 140 @ @ ca 84 @ =end n = gi a, b, c = gis if a == 1 p n exit end if n < a p 0 exit end a_cnt = n.div a b_cnt = n.div b c_cnt = n.div c ab_cnt = n.div (a.lcm b) bc_cnt = n.div (b.lcm c) ca_cnt = n.div (c.lcm a) abc_cnt = n.div (a.lcm b.lcm(c)) answer = case when b % a == 0 && c % a == 0 a_cnt when c % b == 0 || c % a == 0 a_cnt + b_cnt - ab_cnt when b % a == 0 a_cnt + c_cnt - cb_cnt else a_cnt + b_cnt + c_cnt - (ab_cnt + bc_cnt + ca_cnt) + abc_cnt end p answer $stdout = STDERR p n p [a, b, c, a * b, b * c, c * a, a * b * c] p [a_cnt, b_cnt, c_cnt, ab_cnt, bc_cnt, ca_cnt, abc_cnt] p a_cnt + b_cnt + c_cnt # 素直に解く =begin cnt = 0 1.upto(n) do |i| cnt += 1 if i % a == 0 || i % b == 0 || i % c == 0 end p cnt =end