結果

問題 No.546 オンリー・ワン
ユーザー htensaihtensai
提出日時 2020-01-30 19:55:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 2,157 ms
コンパイル使用メモリ 74,116 KB
実行使用メモリ 49,732 KB
最終ジャッジ日時 2023-10-14 08:41:22
合計ジャッジ時間 2,956 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,588 KB
testcase_01 AC 39 ms
49,376 KB
testcase_02 AC 40 ms
49,276 KB
testcase_03 AC 39 ms
49,404 KB
testcase_04 AC 39 ms
49,436 KB
testcase_05 AC 38 ms
49,188 KB
testcase_06 AC 38 ms
49,732 KB
testcase_07 AC 39 ms
49,712 KB
testcase_08 AC 39 ms
49,420 KB
testcase_09 AC 37 ms
49,232 KB
testcase_10 AC 38 ms
49,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 3);
        int n = Integer.parseInt(first[0]);
        int low = Integer.parseInt(first[1]) - 1;
        int high = Integer.parseInt(first[2]);
        String[] line = br.readLine().split(" ", n);
        int[] base = new int[n];
        dp = new int[1 << n];
        for (int i = 0; i < n; i++) {
            base[i] = Integer.parseInt(line[i]);
            dp[1 << i] = base[i];
        }
        long total = 0;
        for (int i = 1; i < (1 << n); i++) {
            dp[i] = dfw(i);
            total += high / dp[i] * getCount(i);
            total -= low / dp[i] * getCount(i);
        }
        System.out.println(total);
    }
    
    static int getCount(int x) {
        int count = 0;
        while (x > 0) {
            count += (x & 1);
            x = x >> 1;
        }
        if (count % 2 == 0) {
            return -count;
        } else {
            return count;
        }
    }
    
    static int dfw(int key) {
        if (dp[key] != 0) {
            return dp[key];
        }
        int x = 1;
        while (true) {
            if ((x & key) == x) {
                return dp[key] = getLCM(dfw(x), dfw(key ^ x));
            }
            x = x << 1;
        }
        //return 0;
    }
    
    static int getLCM(int x, int y) {
        x /= gcd(x, y);
        if ((long) x * (long) y >= Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else {
            return x * y;
        }
    }
    
    static int gcd(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return gcd(y, x % y);
        }
    }
}
0