結果

問題 No.316 もっと刺激的なFizzBuzzをください
ユーザー LeonardoneLeonardone
提出日時 2015-12-09 07:31:11
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 2,429 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 11,308 KB
実行使用メモリ 15,312 KB
最終ジャッジ日時 2023-10-12 22:44:37
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
15,020 KB
testcase_01 AC 78 ms
15,192 KB
testcase_02 AC 78 ms
15,120 KB
testcase_03 AC 80 ms
15,272 KB
testcase_04 AC 79 ms
15,140 KB
testcase_05 AC 79 ms
15,312 KB
testcase_06 AC 79 ms
15,264 KB
testcase_07 RE -
testcase_08 AC 79 ms
15,284 KB
testcase_09 AC 79 ms
15,172 KB
testcase_10 AC 79 ms
15,248 KB
testcase_11 RE -
testcase_12 AC 79 ms
15,124 KB
testcase_13 AC 79 ms
15,196 KB
testcase_14 AC 79 ms
15,132 KB
testcase_15 AC 79 ms
15,240 KB
testcase_16 AC 79 ms
15,216 KB
testcase_17 AC 79 ms
15,196 KB
testcase_18 AC 80 ms
15,200 KB
testcase_19 AC 79 ms
15,160 KB
testcase_20 AC 81 ms
15,176 KB
testcase_21 AC 79 ms
15,220 KB
testcase_22 AC 80 ms
15,136 KB
testcase_23 AC 81 ms
15,176 KB
testcase_24 AC 79 ms
15,100 KB
testcase_25 AC 80 ms
15,308 KB
testcase_26 AC 80 ms
15,040 KB
testcase_27 AC 80 ms
15,212 KB
testcase_28 AC 78 ms
15,184 KB
testcase_29 AC 80 ms
15,200 KB
testcase_30 AC 80 ms
15,220 KB
testcase_31 AC 80 ms
15,276 KB
testcase_32 AC 79 ms
15,308 KB
testcase_33 RE -
testcase_34 AC 80 ms
15,184 KB
testcase_35 AC 80 ms
15,304 KB
testcase_36 AC 79 ms
15,188 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#! 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.sort

if a == 1
    puts n
    exit
end

if n < a
    puts '0'
    exit
end

a_cnt = n.div(a).floor
b_cnt = n.div(b).floor
c_cnt = n.div(c).floor
ab_cnt = n.div(a.lcm(b)).floor
bc_cnt = n.div(b.lcm(c)).floor
ca_cnt = n.div(c.lcm(a)).floor
abc_cnt = n.div(a.lcm(b.lcm(c))).floor

answer = 
    if b % a == 0 && c % a == 0
        a_cnt
    elsif c % b == 0 || c % a == 0
        a_cnt + b_cnt - ab_cnt
    elsif 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

puts answer

=begin
$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
=end


# 素直に解く

=begin 

    cnt = 0
    
    1.upto(n) do |i|
        cnt += 1 if i % a == 0 || i % b == 0 || i % c == 0
    end
    
    p cnt

=end
0