結果
問題 | No.308 素数は通れません |
ユーザー |
![]() |
提出日時 | 2021-05-15 16:36:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,450 bytes |
コンパイル時間 | 2,287 ms |
コンパイル使用メモリ | 202,324 KB |
最終ジャッジ日時 | 2025-01-21 12:59:32 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 98 WA * 9 |
ソースコード
//https://ncode.syosetu.com/n4830bu/308/ #include <bits/stdc++.h> using namespace std; using ll = long long; bool IsPrime(__int128 n) { if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3; __int128 d = n - 1; while (d % 2 == 0) d /= 2; __int128 bases[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; for (__int128 base : bases) { __int128 t = d, _t = t, y = 1; while (_t) { if (_t % 2) (y *= base) %= n; (base *= base) %= n; _t /= 2; } if (base % n) while (t != n - 1 && y != 1 && y != n - 1) { (y *= y) %= n; t *= 2; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } int main() { string S; cin >> S; if (S.size() < 4) { int N = stoi(S); for (int W = 1;; W++) { vector<bool> seen(N + 1); queue<int> que; seen[1] = true; que.push(1); while (!que.empty()) { auto n = que.front(); que.pop(); if (n == N) { cout << W << endl; return 0; } if (n + 1 <= N && n % W != 0) { if (!seen[n + 1] && !IsPrime(n + 1)) { seen[n + 1] = true; que.push(n + 1); } } if (n - 1 >= 1 && n % W != 1) { if (!seen[n - 1] && !IsPrime(n - 1)) { seen[n - 1] = true; que.push(n - 1); } } if (n + W <= N) { if (!seen[n + W] && !IsPrime(n + W)) { seen[n + W] = true; que.push(n + W); } } if (n - W >= 1) { if (!seen[n - W] && !IsPrime(n - W)) { seen[n - W] = true; que.push(n - W); } } } } } else { __int128 N = 0; for (int i = 0; i < S.size() - 1; i++) { N *= 10; N += S[i] - '0'; } if (N % 8 == 1 && IsPrime(N - 8)) cout << 14 << endl; else cout << 8 << endl; } }