結果

問題 No.577 Prime Powerful Numbers
ユーザー ミドリムシミドリムシ
提出日時 2018-04-21 15:06:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,560 bytes
コンパイル時間 590 ms
コンパイル使用メモリ 71,648 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-09 12:18:54
合計ジャッジ時間 3,090 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Xorshift
{
private:
    unsigned x, y, z, w;
public:
    Xorshift(unsigned seed=88675123, int loop=50){
        x = 123456789;
        y = 362436069;
        z = 521288629;
        w = seed;
        while(--loop >= 0){
            (*this)();
        }
    }
    unsigned operator()(){
        unsigned t=(x^(x<<11));
        x=y; y=z; z=w;
        return w=(w^(w>>19))^(t^(t>>8));
    }
    unsigned operator()(unsigned size){
        return (*this)() % size;
    }
    unsigned long long get64(){
        unsigned long long a = (*this)();
        unsigned long long b = (*this)();
        return (a << 32) | b;
    }
    unsigned long long get64(unsigned long long size){
        return get64() % size;
    }
    template <class T>
    void shufflte(vector<T>& v){
        unsigned n = v.size();
        for(unsigned i=0; i<n-1; ++i){
            unsigned j = (*this)(n-i) + i;
            swap(v[i], v[j]);
        }
    }
};
Xorshift xorshift;

unsigned long long modmul(unsigned long long a, unsigned long long b, unsigned long long mod)
{
    unsigned long long ret = 0;
    while(b > 0){
        if(b & 1){
            ret += a;
            if(ret > mod)
                ret -= mod;
        }
        a <<= 1;
        if(a > mod)
            a -= mod;
        b >>= 1;
    }
    return ret;
}

unsigned long long modpow(unsigned long long a, unsigned long long b, unsigned long long mod)
{
    unsigned long long ret = 1;
    unsigned long long tmp = a;
    while(b > 0){
        if(b & 1)
            ret = modmul(ret, tmp, mod);
        tmp = modmul(tmp, tmp, mod);
        b >>= 1;
    }
    return ret;
}

bool millerRabinPrimalityTest(long long n, int loop=30)
{
    if(n < 2)
        return false;

    long long d = n - 1;
    int s = 0;
    while(d % 2 == 0){
        ++ s;
        d /= 2;
    }

    while(--loop >= 0){
        long long a = xorshift.get64(n-1) + 1;
        long long x = modpow(a, d, n);
        if(x != 1){
            bool isComposite = true;
            for(int r=0; r<s; ++r){
                if(x == n - 1){
                    isComposite = false;
                    break;
                }
                x = modmul(x, x, n);
            }
            if(isComposite)
                return false;
        }
    }

    return true;
}

long power(long base, int exponent){
    long ans = 1;
    for(int i = 0; i < exponent; i++){
        ans *= base;
        if(ans > 6e18 / base){
            return 2e18;
        }
    }
    return ans;
}

bool testcase(long N){
    if(N == 2){
        return false;
    }else if(N % 2 == 0){
        return true;
    }
    for(long i = 2; i < N; i *= 2){
        for(int j = 1; j < 100; j++){
            long left = 1, right = 2e18; // [left, right]
            while(left < right){
                long middle = (left + right) / 2;
                long power_num = power(middle, j);
                if(power_num > N - i){
                    right = middle - 1;    
                }else if(power_num < N - i){
                    left = middle + 1;
                }else if(millerRabinPrimalityTest(middle)){
                    return true;
                }else{
                    break;
                }
            }
        }
    }
    return false;
}

int main(){
    int Q;
    cin >> Q;
    for(int i = 0; i < Q; i++){
        long N;
        cin >> N;
        if(testcase(N)){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}
0