結果
| 問題 |
No.546 オンリー・ワン
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-01-22 15:27:59 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 66 ms / 2,000 ms |
| コード長 | 2,189 bytes |
| コンパイル時間 | 6,110 ms |
| コンパイル使用メモリ | 79,916 KB |
| 実行使用メモリ | 51,124 KB |
| 最終ジャッジ日時 | 2025-01-22 15:28:06 |
| 合計ジャッジ時間 | 5,040 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 7 |
ソースコード
import java.io.*;
import java.util.StringTokenizer;
/**
* Date: 2025/1/22 14:14
*/
public class Main {
public static void main(String[] args) {
int n = sc.nextInt(), l = sc.nextInt(), r = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = f(a, n, r) - f(a, n, l - 1);
out.println(ans);
out.close();
}
static long f(int[] a, int n, int t) {
long ans = 0;
for (int s = 1; s < 1 << n; s++) {
int bit = Integer.bitCount(s);
long v = 1;
for (int i = 0; i < n; i++) {
if ((s >> i & 1) == 1) v = getLCM(v, a[i]);
}
if (bit % 2 == 1) {
ans += bit * (t / v);
} else {
ans -= bit * (t / v);
}
}
return ans;
}
//求 a 和 b 的最大公约数,做法:辗转相除法
static long getGCD(long a, long b) {
if (b == 0) return a;
if (a % b == 0) return b;
return getGCD(b, a % b);
}
//求 a 和 b 的最小公倍数, lcm * gcd = a * b
static long getLCM(long a, long b) {
return a * b / getGCD(a, b);
}
static Kattio sc = new Kattio();
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static class Kattio {
static BufferedReader r;
static StringTokenizer st;
public Kattio() {
r = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(r.readLine());
}
return st.nextToken();
} catch (Exception e) {
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}