結果

問題 No.577 Prime Powerful Numbers
ユーザー parukiparuki
提出日時 2017-10-29 13:21:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 2,663 bytes
コンパイル時間 1,779 ms
コンパイル使用メモリ 179,424 KB
実行使用メモリ 13,020 KB
最終ジャッジ日時 2023-08-14 09:31:48
合計ジャッジ時間 3,392 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
12,824 KB
testcase_01 AC 61 ms
12,820 KB
testcase_02 AC 60 ms
12,888 KB
testcase_03 AC 85 ms
12,820 KB
testcase_04 AC 65 ms
12,740 KB
testcase_05 AC 118 ms
12,744 KB
testcase_06 AC 79 ms
13,020 KB
testcase_07 AC 134 ms
12,896 KB
testcase_08 AC 79 ms
12,740 KB
testcase_09 AC 75 ms
12,764 KB
testcase_10 AC 56 ms
12,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

vector<int> sieve(int n) {
    vector<bool> f(n + 1);
    for (int i = 3; i <= n; i += 2) f[i] = 1;
    for (int i = 3; i <= sqrt(n); i += 2) if (f[i]) for (int j = i * 3; j <= n; j += i) f[j] = 0;
    vector<int> res;
    if (n >= 2)res.push_back(2);
    for (int i = 3; i <= n; i += 2) if (f[i]) res.push_back(i);
    return res;
}

ll powMod(__int128 n, ll k, ll mod) {
    if (n == 0)return 0;
    __int128 res = 1;
    while (k) {
        if (k & 1)res = res*n%mod;
        n = n*n%mod;
        k >>= 1;
    }
    return res;
}

bool MillerRabinPrimalityTest(ll n, int precision = 20) {
    static mt19937_64 mt64;
    if (n == 2)return true;
    if (n == 1 || n % 2 == 0)return false;

    ll q = n - 1, k = 0;
    while (!(q & 1)) {
        q >>= 1;
        k++;
    }

    uniform_int_distribution<ll> unirand(1, n - 1);
    for (int i = 0; i < precision; ++i) {
        ll a = unirand(mt64);
        ll aq = powMod(a, q, n);
        if (aq == 1)continue;
        bool flag = true;
        for (int j = 0; j < k; ++j) {
            if (powMod(aq, (1ll << j), n) == n - 1) {
                flag = false;
                break;
            }
        }
        if (flag)return false;
    }

    return true;
}

vi primes;
unordered_set<ll> S;

bool solve(ll N) {
    if (N <= 2)return false;
    if (N % 2 == 0)return true;
    for (ll a = 1; (1ll << a) < N; ++a) {
        ll qb = N - (1ll<<a);
        if (S.count(qb))return true;

        if (MillerRabinPrimalityTest(qb))return true;
        ll sqrt_qb = (ll)sqrt(qb);
        for (ll q = max(3ll, sqrt_qb - 1000); q < sqrt_qb + 1000; ++q) {
            if (q*q == qb && MillerRabinPrimalityTest(q)) return true;
        }
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int Q;
    cin >> Q;
    primes = sieve(1000000);
    each(p, primes) {
        ll a = p;
        for (int i = 2; a*p / p == a && a*p <= (ll)1e18; ++i) {
            a *= p;
            if (i >= 3)S.insert(a);
        }
    }
    while (Q--) {
        ll N;
        cin >> N;
        cout << (solve(N) ? "Yes" : "No") << endl;
    }
}
0