結果

問題 No.308 素数は通れません
ユーザー PachicobuePachicobue
提出日時 2018-04-03 01:38:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,706 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 204,456 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-08 13:51:00
合計ジャッジ時間 5,397 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
testcase_101 -- -
testcase_102 -- -
testcase_103 -- -
testcase_104 -- -
testcase_105 -- -
testcase_106 -- -
権限があれば一括ダウンロードができます

ソースコード

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