結果

問題 No.390 最長の数列
ユーザー 37zigen37zigen
提出日時 2016-07-07 22:38:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 736 ms / 5,000 ms
コード長 4,965 bytes
コンパイル時間 3,214 ms
コンパイル使用メモリ 80,200 KB
実行使用メモリ 49,576 KB
最終ジャッジ日時 2024-10-02 10:24:17
合計ジャッジ時間 9,659 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,040 KB
testcase_01 AC 53 ms
37,040 KB
testcase_02 AC 53 ms
36,980 KB
testcase_03 AC 51 ms
36,884 KB
testcase_04 AC 51 ms
36,608 KB
testcase_05 AC 693 ms
49,336 KB
testcase_06 AC 708 ms
49,516 KB
testcase_07 AC 51 ms
36,960 KB
testcase_08 AC 56 ms
41,060 KB
testcase_09 AC 56 ms
41,124 KB
testcase_10 AC 736 ms
49,340 KB
testcase_11 AC 710 ms
49,576 KB
testcase_12 AC 675 ms
49,360 KB
testcase_13 AC 386 ms
48,848 KB
testcase_14 AC 482 ms
46,200 KB
testcase_15 AC 54 ms
37,140 KB
testcase_16 AC 62 ms
40,724 KB
testcase_17 AC 192 ms
47,004 KB
testcase_18 AC 229 ms
48,440 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