結果

問題 No.316 もっと刺激的なFizzBuzzをください
ユーザー 伟国姚
提出日時 2025-01-22 15:08:23
言語 Java
(openjdk 23)
結果
AC  
実行時間 73 ms / 1,000 ms
コード長 1,969 bytes
コンパイル時間 6,885 ms
コンパイル使用メモリ 80,272 KB
実行使用メモリ 38,088 KB
最終ジャッジ日時 2025-01-22 15:08:40
合計ジャッジ時間 10,296 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.StringTokenizer;

/**
 * Date: 2025/1/22 14:05
 */
public class Main {

    public static void main(String[] args) {
        long n = sc.nextInt();
        long[] a = new long[3];
        for (int i = 0; i < 3; i++) {
            a[i] = sc.nextLong();
        }
        long ans = 0;
        for (int s = 1; s < 1 << 3; s++) {
            long v = 1;
            int bit = Integer.bitCount(s);
            for (int i = 0; i < 3; i++) {
                if ((s >> i & 1) == 1) v =getLCM(v,a[i]);
            }
            if (bit % 2 == 1) ans += n / v;
            else ans -= n / v;
        }
        out.println(ans);

        out.close();
    }

    //求 a 和 b 的最大公约数,做法:辗转相除法
    static long getGCD(long a, long b) {
        if (b == 0) return a;
        if (a % b == 0) return b;
        return getGCD(b, a % b);
    }


    //求 a 和 b 的最小公倍数, lcm * gcd = a * b
    static long getLCM(long a, long b) {
        return a * b / getGCD(a, b);
    }

    static Kattio sc = new Kattio();
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    static class Kattio {
        static BufferedReader r;
        static StringTokenizer st;

        public Kattio() {
            r = new BufferedReader(new InputStreamReader(System.in));
        }

        public String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(r.readLine());
                }
                return st.nextToken();
            } catch (Exception e) {
                return null;
            }
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}


0