結果

問題 No.546 オンリー・ワン
ユーザー takeya_okinotakeya_okino
提出日時 2017-07-14 23:17:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 1,339 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 77,016 KB
実行使用メモリ 41,576 KB
最終ジャッジ日時 2024-04-16 20:32:04
合計ジャッジ時間 4,179 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,576 KB
testcase_01 AC 129 ms
41,128 KB
testcase_02 AC 128 ms
41,172 KB
testcase_03 AC 131 ms
41,260 KB
testcase_04 AC 134 ms
41,572 KB
testcase_05 AC 130 ms
41,324 KB
testcase_06 AC 129 ms
41,264 KB
testcase_07 AC 131 ms
41,276 KB
testcase_08 AC 147 ms
41,136 KB
testcase_09 AC 145 ms
41,372 KB
testcase_10 AC 135 ms
41,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    long L = sc.nextLong();
    long H = sc.nextLong();
    long[] c = new long[N];
    for(int i = 0; i < N; i++) {
      c[i] = sc.nextLong();
    }
    long ans = 0;
    for(int i = 0; i < N; i++) {
      // c[i]のみで割れる個数を求める
      long t = H / c[i] - (L - 1) / c[i];
      long k = 0;
      for(int j = 1; j < (int)Math.pow(2, N); j++) {
        int count = 0;
        boolean flg = false;
        boolean over = true;
        long baisuu = 1;
        for(int l = 0; l < N; l++) {
          if((j & (1 << l)) != 0) {
            if(l == i) flg = true;
            count++;
            long g = gcd(baisuu, c[l]);
            baisuu *= (c[l] / g);
            if(baisuu > (long)Math.pow(10, 9)) {
              over = false;
              break;
            }
          }
        }
        if(over && flg && count > 1) {
          if(count % 2 == 1) {
            k -= (H / baisuu - (L - 1) / baisuu);
          } else {
            k += (H / baisuu - (L - 1) / baisuu);
          }
        }
      }
      ans += (t - k);
    }
    System.out.println(ans);
  }

  public static long gcd(long a, long b) {
    if(b == 0) return a;
    return gcd(b, a % b);
  }
}
0