結果
| 問題 |
No.546 オンリー・ワン
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-07-15 00:50:39 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 146 ms / 2,000 ms |
| コード長 | 1,216 bytes |
| コンパイル時間 | 2,334 ms |
| コンパイル使用メモリ | 77,988 KB |
| 実行使用メモリ | 41,584 KB |
| 最終ジャッジ日時 | 2024-10-08 00:26:59 |
| 合計ジャッジ時間 | 4,767 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 7 |
ソースコード
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
new Main().run();
}
void run() {
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();
}
System.out.println(calc(H, c, n) - calc(L - 1, c, n));
}
long calc(long v, long[] c, int n) {
long ans = 0;
out: for (int s = 1; s < 1 << n; ++s) {
long f = 1;
for (int i = 0; i < n; ++i) {
if ((s & (1L << i)) > 0) {
f = lcm(f, c[i]);
if (f < 0) {
continue out;
}
}
}
if (Integer.bitCount(s) % 2 == 1) {
ans += v / f * Integer.bitCount(s);
} else {
ans -= v / f * Integer.bitCount(s);
}
}
return ans;
}
long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
long gcd(long a, long b) {
if (a < b) {
long tmp = a;
a = b;
b = tmp;
}
if (b == 0) {
return a;
}
return gcd(a % b, b);
}
void tr(Object... objects) {
System.out.println(Arrays.deepToString(objects));
}
}