結果

問題 No.843 Triple Primes
ユーザー GrenacheGrenache
提出日時 2019-06-28 22:22:28
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,848 bytes
コンパイル時間 4,433 ms
コンパイル使用メモリ 77,128 KB
実行使用メモリ 58,132 KB
最終ジャッジ日時 2023-09-14 22:09:54
合計ジャッジ時間 12,067 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,728 KB
testcase_01 AC 149 ms
55,564 KB
testcase_02 AC 132 ms
55,728 KB
testcase_03 AC 130 ms
55,956 KB
testcase_04 AC 130 ms
55,732 KB
testcase_05 AC 132 ms
55,724 KB
testcase_06 AC 132 ms
55,716 KB
testcase_07 AC 150 ms
55,368 KB
testcase_08 AC 148 ms
53,908 KB
testcase_09 AC 147 ms
56,048 KB
testcase_10 AC 146 ms
55,792 KB
testcase_11 AC 152 ms
55,792 KB
testcase_12 AC 149 ms
55,856 KB
testcase_13 AC 150 ms
55,556 KB
testcase_14 AC 150 ms
55,728 KB
testcase_15 AC 148 ms
55,412 KB
testcase_16 AC 150 ms
55,804 KB
testcase_17 WA -
testcase_18 AC 128 ms
56,108 KB
testcase_19 AC 130 ms
55,712 KB
testcase_20 AC 143 ms
55,836 KB
testcase_21 AC 142 ms
55,416 KB
testcase_22 AC 143 ms
55,412 KB
testcase_23 AC 145 ms
55,844 KB
testcase_24 AC 141 ms
55,944 KB
testcase_25 AC 143 ms
55,692 KB
testcase_26 AC 153 ms
55,724 KB
testcase_27 AC 138 ms
55,760 KB
testcase_28 AC 149 ms
56,052 KB
testcase_29 AC 142 ms
55,536 KB
testcase_30 AC 150 ms
55,712 KB
testcase_31 AC 141 ms
55,732 KB
testcase_32 AC 135 ms
55,724 KB
testcase_33 AC 144 ms
55,972 KB
testcase_34 AC 142 ms
55,748 KB
testcase_35 AC 151 ms
53,816 KB
testcase_36 AC 142 ms
56,252 KB
testcase_37 AC 150 ms
56,220 KB
testcase_38 AC 147 ms
55,732 KB
testcase_39 AC 149 ms
55,752 KB
testcase_40 AC 129 ms
55,524 KB
testcase_41 WA -
testcase_42 AC 156 ms
55,716 KB
testcase_43 AC 154 ms
55,828 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 == 0) {
			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