結果

問題 No.577 Prime Powerful Numbers
ユーザー PachicobuePachicobue
提出日時 2017-10-14 19:06:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,266 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 170,072 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:07:17
合計ジャッジ時間 3,479 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 17 ms
5,376 KB
testcase_02 AC 29 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 96 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;


mt19937 mt{random_device{}()};
ll power(const ll p, const ll n, const ll mod)
{
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return (ll)((__int128_t)((__int128_t)power(p, n - 1, mod) * (__int128_t)p) % mod);
    } else {
        const __int128_t pp = power(p, n / 2, mod);
        return (ll)((__int128_t)(pp * pp) % mod);
    }
}
bool isPrime(const ll n)
{
    constexpr int TEST = 1000;
    ll s = 0;
    ll d = n - 1;
    while (d % 2 == 0) {
        s++;
        d /= 2;
    }
    uniform_int_distribution<ll> dist{1, n - 1};
    for (int i = 0; i < TEST; i++) {
        const ll a = dist(mt);
        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 = (ll)((__int128_t)((__int128_t)num * (__int128_t)num) % n);
            }
            if (comp) {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        ll N;
        cin >> N;
        if (N == 1) {
            show(N);
            cout << "No" << endl;
        } else if (N == 2) {
            show(N);
            cout << "No" << endl;
        } else if (N % 2 == 0) {
            cout << "Yes" << endl;
        } else {
            bool ok = false;
            for (int i = 1; (1LL << i) < N; i++) {
                if (ok) {
                    break;
                }
                const ll res = N - (1LL << i);
                for (int j = 1; j <= (int)log2(res * 2 - 1); j++) {
                    if (ok) {
                        break;
                    }
                    const ll rt = (ll)pow(res, 1.0 / j) + 1;
                    if (power(rt, j, (ll)1e18) == res) {
                        if (isPrime(rt)) {
                            ok = true;
                            break;
                        }
                    } else if (power(rt - 1, j, (ll)1e18) == res) {
                        if (isPrime(rt - 1)) {
                            ok = true;
                            break;
                        }
                    }
                }
            }
            if (ok) {
                cout << "Yes" << endl;
            } else {
                show(N);
                cout << "No" << endl;
            }
        }
    }
    return 0;
}

0