結果

問題 No.308 素数は通れません
ユーザー PachicobuePachicobue
提出日時 2018-04-03 01:38:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,706 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 199,372 KB
最終ジャッジ日時 2025-01-05 09:52:38
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 98 WA * 8 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = __int128_t;
istream& operator>>(istream& is, ll& v)
{
    string s;
    is >> s;
    v = 0;
    for (int i = 0; i < (int)s.size(); i++) {
        if (isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
    }
    if (s[0] == '-') { v *= -1; }
    return is;
}
ll mul(const ll a, const ll b, const ll mod)
{
    if (b == 0) { return 0; }
    if (b % 2 == 1) {
        return (mul(a, b - 1, mod) + a) % mod;
    } else {
        return mul(2 * a % mod, b / 2, mod);
    }
}
ll power(const ll a, const ll n, const ll mod)
{
    if (n == 0) { return 1; }
    if (n % 2 == 1) {
        return mul(power(a, n - 1, mod), a, mod);
    } else {
        return power(mul(a, a, mod), n / 2, mod);
    }
}
bool MillerRabin(const ll n)
{
    if (n == 1) { return false; }
    constexpr int TEST = 100;
    ll s = 0, d = n - 1;
    for (; (d & 1) == 0; s++, d >>= 2) {}
    static mt19937 mt{random_device{}()};
    for (int i = 0; i < TEST; i++) {
        const ll a = (mt() % (n - 2)) + 1;
        if (power(a, n - 1, n) != 1) { return false; }
        ll num = power(a, d, n);
        if (num != 1) {
            bool comp = true;
            for (ll r = 0; r < s; r++) {
                if (num == n - 1) {
                    comp = false;
                    break;
                }
                num = mul(num, num, n);
            }
            if (comp) return false;
        }
    }
    return true;
}

int main()
{
    ll N;
    cin >> N;
    cout << (N >= 26 ? N % 8 == 1 and MillerRabin(N - 8) ? 14 : 8 : N == 9 ? 7 : N == 10 ? 7 : N == 15 ? 7 : N == 16 ? 7 : N == 18 ? 8 : N == 21 ? 19 : N == 22 ? 7 : N == 25 ? 23 : (int)N - 1) << endl;
    return 0;
}

0