結果

問題 No.719 Coprime
ユーザー 37zigen37zigen
提出日時 2018-07-29 01:14:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 176 ms / 3,000 ms
コード長 1,721 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 75,936 KB
実行使用メモリ 58,900 KB
最終ジャッジ日時 2023-09-21 01:10:21
合計ジャッジ時間 13,343 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
56,088 KB
testcase_01 AC 122 ms
56,052 KB
testcase_02 AC 122 ms
56,316 KB
testcase_03 AC 124 ms
56,188 KB
testcase_04 AC 121 ms
56,004 KB
testcase_05 AC 122 ms
55,404 KB
testcase_06 AC 122 ms
55,868 KB
testcase_07 AC 122 ms
55,860 KB
testcase_08 AC 123 ms
55,364 KB
testcase_09 AC 122 ms
56,496 KB
testcase_10 AC 122 ms
55,892 KB
testcase_11 AC 122 ms
55,660 KB
testcase_12 AC 123 ms
56,200 KB
testcase_13 AC 122 ms
55,720 KB
testcase_14 AC 124 ms
55,820 KB
testcase_15 AC 123 ms
55,776 KB
testcase_16 AC 126 ms
56,384 KB
testcase_17 AC 123 ms
55,980 KB
testcase_18 AC 125 ms
55,672 KB
testcase_19 AC 124 ms
56,348 KB
testcase_20 AC 121 ms
55,996 KB
testcase_21 AC 126 ms
55,672 KB
testcase_22 AC 124 ms
55,952 KB
testcase_23 AC 126 ms
55,864 KB
testcase_24 AC 124 ms
55,716 KB
testcase_25 AC 126 ms
56,120 KB
testcase_26 AC 125 ms
56,096 KB
testcase_27 AC 128 ms
56,052 KB
testcase_28 AC 123 ms
55,660 KB
testcase_29 AC 127 ms
55,428 KB
testcase_30 AC 129 ms
56,276 KB
testcase_31 AC 127 ms
55,652 KB
testcase_32 AC 127 ms
56,488 KB
testcase_33 AC 127 ms
55,592 KB
testcase_34 AC 126 ms
56,000 KB
testcase_35 AC 128 ms
55,716 KB
testcase_36 AC 129 ms
56,144 KB
testcase_37 AC 128 ms
56,364 KB
testcase_38 AC 130 ms
55,936 KB
testcase_39 AC 128 ms
55,588 KB
testcase_40 AC 130 ms
53,984 KB
testcase_41 AC 133 ms
55,900 KB
testcase_42 AC 134 ms
55,888 KB
testcase_43 AC 135 ms
55,464 KB
testcase_44 AC 136 ms
56,012 KB
testcase_45 AC 134 ms
56,192 KB
testcase_46 AC 142 ms
56,176 KB
testcase_47 AC 142 ms
56,460 KB
testcase_48 AC 145 ms
56,008 KB
testcase_49 AC 144 ms
56,248 KB
testcase_50 AC 144 ms
56,224 KB
testcase_51 AC 144 ms
56,240 KB
testcase_52 AC 145 ms
56,004 KB
testcase_53 AC 147 ms
58,772 KB
testcase_54 AC 168 ms
58,496 KB
testcase_55 AC 173 ms
58,000 KB
testcase_56 AC 170 ms
58,068 KB
testcase_57 AC 174 ms
58,536 KB
testcase_58 AC 176 ms
58,568 KB
testcase_59 AC 175 ms
58,900 KB
testcase_60 AC 174 ms
58,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		boolean[] isPrime = new boolean[1263];
		int[] g = new int[isPrime.length];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		int cnt = 0;
		for (int i = 2; i < isPrime.length; ++i) {
			if (!isPrime[i])
				continue;
			for (int j = 2 * i; j < isPrime.length; j += i) {
				isPrime[j] = false;
			}
			if (i * i <= isPrime.length)
				for (int j = i; j < isPrime.length; j += i) {
					g[j] |= 1 << cnt;
				}
			++cnt;
		}
		ArrayList<Integer> plist = new ArrayList<>();
		for (int i = 0; i < isPrime.length; ++i) {
			if (isPrime[i])
				plist.add(i);
		}
		int N = sc.nextInt();
		boolean[] vis = new boolean[N + 1];
		long[] dp = new long[1 << 11];
		for (int i = N; i > plist.get(10); --i) {
			if (!isPrime[i])
				continue;
			long[] ndp = Arrays.copyOf(dp, dp.length);
			for (int j = i; j <= N; j += i) {
				vis[j] = true;
				for (int s = 0; s < 1 << 11; ++s) {
					if ((s & g[j]) == 0) {
						ndp[s | g[j]] = Math.max(ndp[s | g[j]], dp[s] + j);
					}
				}
			}
			dp = ndp;
		}

		for (int i = 2; i <= N; ++i) {
			if (vis[i])
				continue;
			for (int s = 0; s < 1 << 11; ++s) {
				if ((s & g[i]) == 0)
					dp[s | g[i]] = Math.max(dp[s | g[i]], dp[s] + i);
			}
		}
		long ans = 0;
		for (long v : dp)
			ans = Math.max(ans, v);
		System.out.println(ans);
	}

	long gcd(long a, long b) {
		if (a > b)
			return gcd(b, a);
		if (a == 0)
			return b;
		return gcd(a, b % a);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0