結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
ats5515
|
| 提出日時 | 2017-10-14 00:36:22 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 86 ms / 2,000 ms |
| コード長 | 3,094 bytes |
| コンパイル時間 | 1,069 ms |
| コンパイル使用メモリ | 112,288 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-17 18:27:35 |
| 合計ジャッジ時間 | 1,862 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
ソースコード
#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;
int MOD = 1000000007;
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>>;
#define int long long
int powi(int a, int b) {
if (b == 0) {
return 1;
}
int res = powi(a, b / 2);
if (b % 2 == 0) {
return res*res;
}
else {
return res*res*a;
}
}
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((int)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;
}
signed main() {
//cerr << isPrimeMillerRabin(129, 50) << endl;
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
//T = 200;
//cerr << (int)pow((int)powi(999, 6), 1/(double)1) << endl;
string res;
int m;
int x;
int aa;
while (T--) {
int N;
cin >> N;
//N = powi(MOD, 2) + powi(2, 1);
//cerr << N << endl;
//N = MOD;
if (N % 2 == 0) {
if (N == 2) {
res = "No";
}
else {
res = "Yes";
}
}
else {
m = 2;
res = "No";
while (N - m > 1 && m > 1) {
x = 1;
while (x < 60)
{
if (x == 1) {
aa = N - m;
}
else {
aa = (int)(pow(N - m, 1 / (double)(x)));
}
//cerr << N - m << " " << x << " " << aa << endl;
//if (aa <= 1)break;
//cerr << aa << endl;
for (int j = aa - 3; j < aa + 3; j++) {
if (powi(j, x) == N - m) {
//cerr << j << " " << x << " " << N - m << endl;
if (isPrime(j)) {
//cerr << m << " " << x << endl;
res = "Yes";
break;
}
}
}
x++;
}
if (res == "Yes")break;
m *= 2;
}
}
cout << res << endl;
}
//cout << res << endl;
}
ats5515