結果

問題 No.577 Prime Powerful Numbers
ユーザー yosupotyosupot
提出日時 2017-10-13 23:40:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 2,672 bytes
コンパイル時間 997 ms
コンパイル使用メモリ 110,608 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 05:02:06
合計ジャッジ時間 2,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 12 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 43 ms
4,376 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 214 ms
4,376 KB
testcase_06 AC 19 ms
4,380 KB
testcase_07 AC 220 ms
4,376 KB
testcase_08 AC 42 ms
4,376 KB
testcase_09 AC 37 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <random>
#include <vector>
#include <array>
#include <bitset>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

struct rng {
    struct A {
        int n;
        const bool operator!=(A r) { return n != r.n; }
        A& operator++() { n++; return *this; }
        int operator*() { return n; }
    };
    int l, r;
    rng(int r) : l(0), r(max(0, r)) {}
    rng(int l, int r) : l(l), r(max(l, r)) {}
    A begin() { return A{l}; }
    A end() { return A{r}; }
};

template<class T>
T pow(T x, ll n, T r) {
    while (n) {
        if (n & 1) r *= x;
        x *= x;
        n >>= 1;
    }
    return r;
}
template<class T> T pow(T x, ll n) { return pow(x, n, T(1)); }

template<class T>
T pow_mod(T x, ll n, T md) {
    T r = 1;
    while (n) {
        if (n & 1) r = (r*x)%md;
        x = (x*x)%md;
        n >>= 1;
    }    
    return r % md;
}

bool isPrime(ll n) {
    if (n <= 1) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    ll d = n-1;
    while (d % 2 == 0) d /= 2;
//    vector<ll> alist{2,3,1662803}; // n < 1.12e12
    vector<ll> alist{2,3,5,7,11,13,17,19,23,29,31,37}; // n < 2^64
    for (ll a: alist) {
        if (n <= a) break;
        ll t = d;
        ll y = pow_mod<__int128>(a, t, n); //over
        while (t != n-1 && y != 1 && y != n-1) {
            y = __int128(y)*y % n; //flow
            t <<= 1;
        }
        if (y != n-1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}

bool ck2(ll x) {
    if (isPrime(x)) return true;
    for (int i = 2; i < 100; i++) {
        ll y = ll(pow(x, 1.0/i) + 0.01);
        if (!isPrime(y)) continue;
        ll z = 1;
        for (int j: rng(i)) z *= y;
        if (z == x) return true;
    }
    return false;
}

bool check(ll x) {
    if (x == 2) return false;
    if (x % 2 == 0) return true;
    ll b = 2;
    while (b < x) {
        if (ck2(x-b)) return true;
        b *= 2;
    }
    return false;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);

    int q;
    cin >> q;
    for (int i: rng(q)) {
        ll x;
        cin >> x;
        if (check(x)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
0