結果

問題 No.390 最長の数列
ユーザー 37zigen37zigen
提出日時 2016-07-07 22:38:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 758 ms / 5,000 ms
コード長 4,965 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 88,000 KB
実行使用メモリ 62,556 KB
最終ジャッジ日時 2024-04-10 08:42:03
合計ジャッジ時間 8,954 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,348 KB
testcase_01 AC 54 ms
50,144 KB
testcase_02 AC 54 ms
50,484 KB
testcase_03 AC 54 ms
50,400 KB
testcase_04 AC 54 ms
50,372 KB
testcase_05 AC 711 ms
60,968 KB
testcase_06 AC 758 ms
62,556 KB
testcase_07 AC 53 ms
50,204 KB
testcase_08 AC 56 ms
54,644 KB
testcase_09 AC 55 ms
54,328 KB
testcase_10 AC 720 ms
61,072 KB
testcase_11 AC 682 ms
61,296 KB
testcase_12 AC 664 ms
61,024 KB
testcase_13 AC 385 ms
60,388 KB
testcase_14 AC 512 ms
56,964 KB
testcase_15 AC 54 ms
50,540 KB
testcase_16 AC 60 ms
54,700 KB
testcase_17 AC 189 ms
59,356 KB
testcase_18 AC 210 ms
60,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

	static void solver() {
		int n = ni();
		int[] x = new int[n];

		for (int i = 0; i < n; i++) {
			x[i] = ni();
		}
		Arrays.sort(x);
		ArrayList<Integer> primeList = primeList((int) Math.sqrt(x[x.length - 1]));

		int[] dp = new int[x[x.length - 1] + 1];

		int ans = 0;
		for (int i = 0; i < n; i++) {
			ArrayList<Long> div = enum_div(primeFactorF(primeList, x[i]));
			long max = 0;
			for (long d : div) {
				if (d == x[i])
					continue;
				if (dp[(int) d] != 0) {
					max = Math.max(max, dp[(int) d]);
				}
			}
			dp[x[i]] = (int) (max + 1);
			ans = Math.max(ans, dp[x[i]]);
		}
		out.println(ans);

	}

	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		long cur = System.currentTimeMillis();
		out = new PrintWriter(System.out);

		solver();
//		out.println(System.currentTimeMillis() - cur + " ms");
		out.flush();
	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max + 1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for (int i = 2; i * i <= max; i++) {
			if (isPrime[i]) {
				for (int j = 2; j * i <= max; j++) {
					isPrime[j * i] = false;
				}
			}
		}
		return isPrime;
	}

	static ArrayList<Integer> primeList(int max) {
		boolean[] isPrime = isPrimeArray(max);
		ArrayList<Integer> primeList = new ArrayList<Integer>();
		for (int i = 2; i <= max; i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static ArrayList<Factor> primeFactorF(ArrayList<Integer> primeList, long num) {
		ArrayList<Factor> ret = new ArrayList<Factor>();
		for (int p : primeList) {
			int exp = 0;
			while (num % p == 0) {
				num /= p;
				exp++;
			}
			if (exp > 0)
				ret.add(new Factor(p, exp));
		}
		if (num > 1)
			ret.add(new Factor(num, 1));
		return ret;
	}

	static ArrayList<Long> enum_div(ArrayList<Factor> f) {
		ArrayList<Long> ret = new ArrayList<Long>();
		ret.add(1L);
		for (int i = 0; i < f.size(); i++) {
			long a = 1;
			int n = ret.size();
			for (int j = 0; j < f.get(i).exp; j++) {
				a *= f.get(i).base;
				for (int k = 0; k < n; k++) {
					ret.add(ret.get(k) * a);
				}
			}
		}
		return ret;
	}

	static class Factor {
		long base, exp;

		Factor(long base, long exp) {
			this.base = base;
			this.exp = exp;
		}
	}

}
0