結果

問題 No.843 Triple Primes
ユーザー GrenacheGrenache
提出日時 2019-06-28 22:23:01
言語 Java19
(openjdk 21)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 3,848 bytes
コンパイル時間 4,837 ms
コンパイル使用メモリ 80,160 KB
実行使用メモリ 57,864 KB
最終ジャッジ日時 2023-10-19 17:46:27
合計ジャッジ時間 12,827 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,616 KB
testcase_01 AC 153 ms
57,664 KB
testcase_02 AC 137 ms
57,840 KB
testcase_03 AC 136 ms
57,704 KB
testcase_04 AC 139 ms
57,692 KB
testcase_05 AC 136 ms
57,532 KB
testcase_06 AC 137 ms
57,656 KB
testcase_07 AC 153 ms
57,640 KB
testcase_08 AC 164 ms
57,616 KB
testcase_09 AC 156 ms
57,556 KB
testcase_10 AC 151 ms
57,412 KB
testcase_11 AC 153 ms
57,368 KB
testcase_12 AC 154 ms
57,540 KB
testcase_13 AC 153 ms
57,580 KB
testcase_14 AC 153 ms
57,628 KB
testcase_15 AC 155 ms
57,604 KB
testcase_16 AC 156 ms
57,560 KB
testcase_17 AC 130 ms
57,584 KB
testcase_18 AC 131 ms
57,496 KB
testcase_19 AC 133 ms
57,512 KB
testcase_20 AC 148 ms
57,700 KB
testcase_21 AC 149 ms
57,612 KB
testcase_22 AC 152 ms
57,804 KB
testcase_23 AC 153 ms
57,600 KB
testcase_24 AC 149 ms
57,516 KB
testcase_25 AC 149 ms
57,624 KB
testcase_26 AC 145 ms
56,812 KB
testcase_27 AC 140 ms
57,764 KB
testcase_28 AC 154 ms
57,608 KB
testcase_29 AC 150 ms
57,864 KB
testcase_30 AC 157 ms
57,728 KB
testcase_31 AC 139 ms
57,224 KB
testcase_32 AC 140 ms
57,432 KB
testcase_33 AC 150 ms
57,628 KB
testcase_34 AC 154 ms
57,528 KB
testcase_35 AC 157 ms
57,500 KB
testcase_36 AC 146 ms
57,244 KB
testcase_37 AC 156 ms
57,808 KB
testcase_38 AC 158 ms
57,560 KB
testcase_39 AC 164 ms
57,632 KB
testcase_40 AC 134 ms
57,504 KB
testcase_41 AC 133 ms
57,480 KB
testcase_42 AC 154 ms
57,464 KB
testcase_43 AC 154 ms
57,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder843 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();
		
		if (n == 2) {
			pr.println(1);
			return;
		} else if (n == 1) {
			pr.println(0);
			return;
		}
		
		Prime prime = new Prime(2 * n);

		long ans = 1;
		for (int r : prime) {
			if (r * r > 2 * n) {
				break;
			}
			if (r == 2) {
				continue;
			}

			int q = r * r - 2;
			if (q <= n && prime.isPrime(q)) {
				ans += 2;
			}
		}
		
		pr.println(ans);
	}

	/**
	 * 素数リスト生成、素数判定、素因数分解などを行う
	 */
	static class Prime implements Iterable<Integer> {
		private int n;
		private BitSet p;

		/**
		 * 上限 n までの素数を保持するインスタンスを返す
		 * O(n log log n)
		 * 
		 * @param n 保持する素数の上限
		 */
		Prime(int n) {
			this.n = n;

			p = new BitSet(n / 2);
			int m = (int)Math.sqrt(n);

//			for (int i = 1; i <= m; i++) {
			for (int bi = p.nextClearBit(0); 2 * (bi + 1) + 1 <= m; bi = p.nextClearBit(bi + 1)) {
				long i = bi + 1;
//				if (p.get(i - 1)) {
//					continue;
//				}

				for (long j = 2 * i * (i + 1); 2 * j + 1 <= n; j += 2 * i + 1) {
					p.set((int)(j - 1));
				}
			}
		}

		/**
		 * 素数かどうかを判定する
		 * O(1)
		 * 
		 * @param n 判定する値
		 * @return 素数の場合、true
		 */
		boolean isPrime(int n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			return !p.get(n / 2 - 1);
		}

		/**
		 * 素数を含む List を返す
		 * O(pi(n)) (pi(n):nまでの素数の個数)
		 * 
		 * @return n 以下の素数を含む List
		 */
		List<Integer> getPrimeList() {
			List<Integer> ret = new ArrayList<>();
			for (int p : this) {
				ret.add(p);
			}

			return ret;
		}

		/**
		 * 素因数分解の結果を返す
		 * O(pi(n^1/2))?
		 * 
		 * @param x 素因数分解をする値
		 * @return 素因数分解した結果
		 */
		Map<Long, Integer> primeFactorize(long x) {
			Map<Long, Integer> hm = new TreeMap<>();
			long n = x;
			for (int p : this) {
				if ((long)p * p > n) {
					break;
				}
				int cnt = 0;
				while (n % p == 0) {
					cnt++;
					n /= p;
				}
				if (cnt > 0) {
					hm.put((long)p, cnt);
				}
			}
			if (n != 1) {
				hm.put(n, 1);
			}

			return hm;
		}

		@Override
		public Iterator<Integer> iterator() {
			return new PrimeIterator();
		}

		private class PrimeIterator implements Iterator<Integer> {
			int index;

			PrimeIterator() {
				index = -1;
			}

			@Override
			public boolean hasNext() {
				if (index == -1) {
					return n >= 2;
				} else {
					return 2 * (index + 1) + 1 <= n;
				}
			}

			@Override
			public Integer next() {
				if (index == -1) {
					if (n >= 2) {
						index = p.nextClearBit(0);
						return 2;
					} else {
						throw new NoSuchElementException();
					}
				} else {
					int ret = 2 * (index + 1) + 1;

					if (ret <= n) {
						index = p.nextClearBit(index + 1);

						return ret;
					} else {
						throw new NoSuchElementException();
					}
				}
			}
		}

		/**
		 * 素数かどうかを判定する(long)
		 * O(n^1/2)
		 * 
		 * @param n 判定する値
		 * @return 素数の場合、true
		 */
		static boolean isPrime(long n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			for (long i = 3; i * i <= n; i += 2) {
				if (n % i == 0) {
					return false;
				}
			}

			return true;
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0