結果

問題 No.577 Prime Powerful Numbers
ユーザー ats5515ats5515
提出日時 2017-10-14 00:36:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 3,094 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 111,836 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 05:08:20
合計ジャッジ時間 2,025 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 7 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 24 ms
4,380 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 99 ms
4,376 KB
testcase_06 AC 15 ms
4,376 KB
testcase_07 AC 107 ms
4,384 KB
testcase_08 AC 22 ms
4,384 KB
testcase_09 AC 20 ms
4,380 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;
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;
}
0