結果

問題 No.546 オンリー・ワン
ユーザー tentententen
提出日時 2023-06-14 10:17:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,802 bytes
コンパイル時間 2,331 ms
コンパイル使用メモリ 86,568 KB
実行使用メモリ 50,020 KB
最終ジャッジ日時 2023-09-04 23:24:08
合計ジャッジ時間 3,706 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,456 KB
testcase_01 AC 41 ms
49,296 KB
testcase_02 AC 42 ms
49,488 KB
testcase_03 AC 42 ms
49,492 KB
testcase_04 AC 42 ms
49,408 KB
testcase_05 AC 41 ms
49,780 KB
testcase_06 AC 42 ms
50,020 KB
testcase_07 AC 43 ms
49,540 KB
testcase_08 AC 44 ms
49,600 KB
testcase_09 AC 44 ms
49,408 KB
testcase_10 AC 44 ms
47,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int left = sc.nextInt();
        int right = sc.nextInt();
        long[] values = new long[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        long[] dp = new long[1 << n];
        dp[0] = 1;
        long ans = 0;
        for (int i = 1; i < (1 << n); i++) {
            int pop = getPop(i);
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) > 0) {
                    dp[i] = getLCM(dp[i ^ (1 << j)], values[j]);
                    break;
                }
            }
            long current = right / dp[i] - (left - 1) / dp[i];
            if (pop % 2 == 0) {
                ans -= current * pop;
            } else {
                ans += current * pop;
            }
        }
        System.out.println(ans);
    }
    
    static long getLCM(long x, long y) {
        long ans = x / getGCD(x, y) * y;
        if (ans >= Integer.MAX_VALUE / 2) {
            return Integer.MAX_VALUE / 2;
        } else {
            return ans;
        }
    }
    
    static long getGCD(long x, long y) {
        if (y == 0) {
            return x;
        } else {
            return getGCD(y, x % y);
        }
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0