結果

問題 No.577 Prime Powerful Numbers
ユーザー mamekinmamekin
提出日時 2017-11-26 16:03:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,146 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 101,700 KB
実行使用メモリ 14,524 KB
最終ジャッジ日時 2023-08-18 07:56:30
合計ジャッジ時間 7,369 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

unsigned xorshift()
{
    static unsigned x=123456789, y=362436069, z=521288629, w=88675123;
    unsigned t=(x^(x<<11));
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

unsigned long long xorshift64()
{
    unsigned long long a = xorshift();
    a <<= 32;
    a |= xorshift();
    return a;
}

long long modmul(long long a, long long b, long long mod)
{
    long long ret = 0;
    for(int i=62; i>=0; --i){
        ret *= 2;
        if(a & (1LL << i))
            ret += b;
        ret %= mod;
    }
    return ret;
}

long long modpow(long long a, long long b, long long mod)
{
    long long ret = 1;
    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=50)
{
    long long d = n - 1;
    int s = 0;
    while(d % 2 == 0){
        ++ s;
        d /= 2;
    }

    while(--loop >= 0){
        long long a = xorshift64() % (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 long power(long long a, int b)
{
    long long ret = 1;
    long long tmp = a;
    while(b > 0){
        if(b & 1)
            ret *= tmp;
        tmp *= tmp;
        b >>= 1;
    }
    return ret;
}

long long nthRoot(long long x, int n)
{
    long long a = 0;
    long long b = x;
    while(a < b){
        long long c = (a + b + 1) / 2;
        if(power(c, n) <= x)
            a = c;
        else
            b = c - 1;
    }
    return a;
}

bool solve(long long n)
{
    if(n % 2 == 0){
        if(n == 2)
            return false;
        else
            return true;
    }

    for(long long pa=2; pa<n; pa*=2){
        long long qb = n - pa;
        for(int b=1; ; ++b){
            long long q = nthRoot(qb, b);
            if(q == 1)
                break;
            if(power(q, b) != qb)
                continue;

            long long qb2 = 1;
            for(int i=0; i<b; ++i)
                qb2 *= q;
            if(qb == qb2 && millerRabinPrimalityTest(q))
                return true;
        }
    }

    return false;
}

int main()
{
    int q;
    cin >> q;

    while(--q >= 0){
        long long n;
        cin >> n;
        if(solve(n))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }

    return 0;
}
0