結果

問題 No.546 オンリー・ワン
ユーザー 37zigen37zigen
提出日時 2017-07-15 00:50:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,216 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 78,164 KB
実行使用メモリ 41,456 KB
最終ジャッジ日時 2024-04-16 20:55:43
合計ジャッジ時間 4,101 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
41,436 KB
testcase_01 AC 128 ms
41,212 KB
testcase_02 AC 113 ms
41,456 KB
testcase_03 AC 134 ms
41,336 KB
testcase_04 AC 128 ms
41,216 KB
testcase_05 AC 127 ms
41,260 KB
testcase_06 AC 125 ms
41,080 KB
testcase_07 AC 129 ms
41,028 KB
testcase_08 AC 132 ms
41,116 KB
testcase_09 AC 132 ms
41,028 KB
testcase_10 AC 143 ms
41,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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));
	}
}
0