結果
問題 | No.546 オンリー・ワン |
ユーザー |
![]() |
提出日時 | 2020-09-08 08:31:23 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 132 ms / 2,000 ms |
コード長 | 1,509 bytes |
コンパイル時間 | 2,639 ms |
コンパイル使用メモリ | 77,120 KB |
実行使用メモリ | 41,392 KB |
最終ジャッジ日時 | 2024-11-29 11:47:54 |
合計ジャッジ時間 | 4,383 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 7 |
ソースコード
import java.util.*; public class Main { static final int MAX = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long left = sc.nextInt(); long right = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] lcms = new int[1 << n]; lcms[0] = 1; long total = 0; for (int i = 1; i < (1 << n); i++) { int pop = getPopcount(i); for (int j = 0; j < n; j++) { if ((i & (1 << j)) != (1 << j)) { continue; } lcms[i] = getLCM(lcms[i ^ (1 << j)], arr[j]); break; } if (pop % 2 == 0) { total -= (right / lcms[i] - (left - 1) / lcms[i]) * pop; } else { total += (right / lcms[i] - (left - 1) / lcms[i]) * pop; } } System.out.println(total); } static int getLCM(int x, int y) { int gcd = getGCD(x, y); if ((long)x / gcd * y >= MAX) { return MAX; } else { return x / gcd * y; } } static int getGCD(int x, int y) { if (x % y == 0) { return y; } else { return getGCD(y, x % y); } } static int getPopcount(int x) { int count = 0; while(x > 0) { count += x % 2; x /= 2; } return count; } }