結果

問題 No.577 Prime Powerful Numbers
ユーザー Kmcode1Kmcode1
提出日時 2017-10-14 00:28:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 5,649 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 179,356 KB
実行使用メモリ 27,532 KB
最終ジャッジ日時 2024-04-28 21:58:10
合計ジャッジ時間 6,222 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:244:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  244 |                 scanf("%lld", &n);
      |                 ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

struct Rho {
	mt19937 mt;
	//Miller miller;
	long long c;
	Rho() {
		mt.seed(clock());
	}
	inline long long f(long long x, long long n) {
		return (x * x + c) % n;
	}
	long long check(long long n) {
		if (n == 4) {
			return 2;
		}
		c = mt() % n;
		long long x = mt() % n;
		long long y = x;
		long long d = 1;
		while (d == 1) {
			x = f(x, n);
			y = f(f(y, n), n);
			d = __gcd(abs(x - y), n);
		}
		if (d == n) {
			return -1;
		}
		return d;
	}
	/*vector<long long> factor(long long n) {
		if (n <= 1) {
			return {};
		}
		if (miller.check(n)) {
			return { n };
		}
		long long res = -1;
		while (res == -1) {
			res = check(n);
		}
		vector<long long> fa = factor(res);
		vector<long long> fb = factor(n / res);
		fa.insert(fa.end(), fb.begin(), fb.end());
		return fa;
	}*/
};

int q;


Rho rrr;

bool ok(long long int val, int j) {
	long long int r = 1;
	while (j--) {
		r *= val;
		if (r > val) {
			return false;
		}
	}
	if (r > val) {
		return false;
	}
	return true;
}
long long int sq(long long int w, int j) {
	if (j == 1) {
		return w;
	}
	if (j == 2) {
		return sqrt(w);
	}
	return -1;
}
long long int pp(long long int w, int j) {
	long long int r = 1;
	while (j--) {
		r *= w;
	}
	return r;
}
vector<int> primes;
vector<int> smallestPrimeFactor;
void linearSieve(int n) {
	if (n < 1) n = 1;
	if ((int)smallestPrimeFactor.size() >= n + 1) return;
	int primePiBound = n < 20 ? n - 1 : (int)(n / (log(n * 1.) - 2) + 2);
	primes.assign(primePiBound + 1, numeric_limits<int>::max());
	int P = 0;
	smallestPrimeFactor.assign(n + 1, 0);
	smallestPrimeFactor[1] = 1;
	int n2 = n / 2, n3 = n / 3, n5 = n / 5;
	if (n >= 2)
		primes[P++] = 2;
	if (n >= 3)
		primes[P++] = 3;
	for (int q = 2; q <= n; q += 2)
		smallestPrimeFactor[q] = 2;
	for (int q = 3; q <= n; q += 6)
		smallestPrimeFactor[q] = 3;
	for (int q = 5; q <= n5; q += 2) {
		if (smallestPrimeFactor[q] == 0)
			primes[P++] = smallestPrimeFactor[q] = q;
		int bound = smallestPrimeFactor[q];
		for (int i = 2; ; ++i) {
			int p = primes[i];
			if (p > bound) break;
			int pq = p * q;
			if (pq > n) break;
			smallestPrimeFactor[pq] = p;
		}
	}
	for (int q = (n5 + 1) | 1; q <= n; q += 2) {
		if (smallestPrimeFactor[q] == 0)
			primes[P++] = smallestPrimeFactor[q] = q;
	}
	primes.resize(P);
}

typedef int FactorsInt;
typedef vector<pair<FactorsInt, int> > Factors;
void primeFactors(FactorsInt x, Factors &out_v) {
	linearSieve(x);
	out_v.clear();
	while (x != 1) {
		int p = smallestPrimeFactor[x], k = 0;
		x /= p, k++;
		while (x % p == 0) x /= p, k++;
		out_v.push_back(make_pair(p, k));
	}
}


void divisors(int num, vector<FactorsInt> &v) {
	Factors pf; primeFactors(num, pf);
	queue<pair<int, int> > stk;
	stk.push(make_pair(1, 0));
	while (!stk.empty()) {
		pair<int, int> f = stk.front();
		stk.pop();
		if (f.second == pf.size()) {
			continue;
		}
		int cur = f.first;
		stk.push(make_pair(f.first, f.second + 1));
		for (int i = 1; i <= pf[f.second].second; i++) {
			cur *= pf[f.second].first;
			v.push_back(cur);
			stk.push(make_pair(cur, f.second + 1));
		}
	}
	v.push_back(1);
}
unordered_set<long long int> s;
bool pr[1000002];

bool isprime(long long int val) {
	for (int i = 0; i < primes.size() && primes[i] * primes[i] <= val; i++) {
		if (val%primes[i] == 0) {
			return false;
		}
	}
	return true;
}
long long int mult_mod(long long int A, long long int B, long long int MOD) {
	if (B == 0LL) {
		return 0;
	}
	long long int u = mult_mod(A, B >> 1LL, MOD);
	u <<= 1LL;
	if (u >= MOD)u %= MOD;
	if (B & 1LL)u += A;
	if (u >= MOD)u %= MOD;
	return u;
}
struct Miller {
	const vector<long long> v = { 2 , 7 , 61 }; // < 4,759,123,141
												// x^k (mod m)
	long long modpow(long long x, long long k, long long m) {
		long long res = 1;
		while (k) {
			if (k & 1) {
				res = mult_mod(res, x, m);
			}
			k /= 2;
			x = mult_mod(x, x, m);
		}
		return res;
	}
	// check if n is prime
	bool check(long long n) {
		if (n < 2) {
			return false;
		}
		long long d = n - 1;
		long long s = 0;
		while (d % 2 == 0) {
			d /= 2;
			s++;
		}
		for (long long a : v) {
			if (a == n) {
				return true;
			}
			if (modpow(a, d, n) != 1) {
				bool ok = true;
				for (long long r = 0; r < s; r++) {
					if (modpow(a, d * (1LL << r), n) == n - 1) {
						ok = false;
						break;
					}
				}
				if (ok) {
					return false;
				}
			}
		}
		return true;
	}
};

Miller miller;

int main() {
	linearSieve(1000002);
	for (int i = 0; i < primes.size(); i++) {
		pr[primes[i]] = true;
		long long int val = 1;
		while (val <= 1000000000000000000LL) {
			s.insert(val);
			long long int pp2 = val;
			val *= primes[i];
			if (val / primes[i] != pp2) {
				break;
			}
		}
	}
	s.erase(1);
	cin >> q;
	while (q--) {
		long long int n;
		scanf("%lld", &n);
		if (n <= 2) {
			puts("No");
			continue;
		}
		if (n % 2 == 0LL) {
			puts("Yes");
			continue;
		}
		long long int r = 2;
		bool ok = false;
		while (r <= n&&r >= 0) {
			long long int rest = n - r;
			if (rest <= 1) {
				break;
			}
			if (s.count(rest)) {
				ok = true;
				break;
			}
			for (int j = 2; j >= 1; j--) {
				long long int k = sq(rest, j);
				if (k <= 1)continue;
				if (pp(k, j) == rest) {
					j = 0;
					if (k <= 1000000 && pr[k]) {
						ok = true;
						break;
					}
					else {
						if (k <= 1000000 && pr[k] == false) {
							continue;
						}
					}
					if (isprime(k) == false) {
						continue;
					}
					if (rrr.check(k)==-1) {
						ok = true;
						break;
					}
					//return 1;
				}
			}
			if (ok) {
				break;
			}
			r *= 2LL;
		}
		if (ok) {
			puts("Yes");
		}
		else {
			puts("No");
		}
	}
	return 0;
}
0