結果
| 問題 | No.546 オンリー・ワン |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-09-08 08:31:23 |
| 言語 | Java (openjdk 25.0.2) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,509 bytes |
| 記録 | |
| コンパイル時間 | 1,590 ms |
| コンパイル使用メモリ | 82,908 KB |
| 実行使用メモリ | 41,804 KB |
| 最終ジャッジ日時 | 2026-05-23 07:29:54 |
| 合計ジャッジ時間 | 3,191 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
}
}
tenten