結果

問題 No.577 Prime Powerful Numbers
コンテスト
ユーザー ミドリムシ
提出日時 2018-04-21 15:54:31
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,499 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 445 ms
コンパイル使用メモリ 78,128 KB
最終ジャッジ日時 2026-03-14 12:40:29
合計ジャッジ時間 954 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp:7:14: error: 'uint64_t' was not declared in this scope
    7 | bool is_SPRP(uint64_t n, uint64_t a) {
      |              ^~~~~~~~
main.cpp:5:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    4 | #include <functional>
  +++ |+#include <cstdint>
    5 | using namespace std;
main.cpp:7:26: error: 'uint64_t' was not declared in this scope
    7 | bool is_SPRP(uint64_t n, uint64_t a) {
      |                          ^~~~~~~~
main.cpp:7:26: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp:7:36: error: expression list treated as compound expression in initializer [-fpermissive]
    7 | bool is_SPRP(uint64_t n, uint64_t a) {
      |                                    ^
main.cpp:19:15: error: 'uint64_t' was not declared in this scope
   19 | bool is_prime(uint64_t x) {
      |               ^~~~~~~~
main.cpp:19:15: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
main.cpp: In function 'bool is_prime_exponentiation(long int)':
main.cpp:50:30: error: 'is_prime' cannot be used as a function
   50 |             }else if(is_prime(middle)){
      |                      ~~~~~~~~^~~~~~~~

ソースコード

diff #
raw source code

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

bool is_SPRP(uint64_t n, uint64_t a) {
  uint64_t s = __builtin_ctzll(n - 1), d = (n - 1) >> s, cur = 1;
  for (uint64_t pw = d; pw; pw >>= 1) {
    if (pw & 1) cur = ((__uint128_t)cur * a) % n;
    a = ((__uint128_t)a * a) % n;
  }
  if (cur == 1) return true;
  for (; s--; cur = ((__uint128_t)cur * cur) % n)
    if (cur == n - 1) return true;
  return false;
}

bool is_prime(uint64_t x) {
  if (x <= 1) return false;
  vector<uint64_t> bases{2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  if (x < 1ull << 32) bases = {2, 7, 61};

  for (const uint64_t base : bases)
    if (x != base && !is_SPRP(x, base)) return false;
  return true;
}

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

bool is_prime_exponentiation(long X){
    for(int i = 1; i < 100; i++){
        long left = 1, right = 2e18; // [left, right]
        while(left <= right){
            long middle = (left + right) / 2;
            long power_num = power(middle, i);
            if(power_num > X){
                right = middle - 1;    
            }else if(power_num < X){
                left = middle + 1;
            }else if(is_prime(middle)){
                return true;
            }else{
                break;
            }
        }
    }
    return false;
}

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 < 2; 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(is_prime_exponentiation(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