結果
問題 | No.577 Prime Powerful Numbers |
ユーザー | Kmcode1 |
提出日時 | 2017-10-13 23:12:48 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,675 bytes |
コンパイル時間 | 2,523 ms |
コンパイル使用メモリ | 219,952 KB |
実行使用メモリ | 22,036 KB |
最終ジャッジ日時 | 2024-11-17 18:10:04 |
合計ジャッジ時間 | 4,286 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | AC | 100 ms
22,024 KB |
testcase_03 | RE | - |
testcase_04 | WA | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | AC | 109 ms
21,900 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; int q; class Rho { long long int mult(long long int A, long long int B, long long int n) { if (B == 0LL) { return 0; } long long int u = mult(A, B >> 1LL, n); u <<= 1LL; if (u >= n)u %= n; if (B & 1LL)u += A; if (u >= n)u %= n; return u; } long long int f(long long int x, long long int mod) { x %= mod; x = mult(x, x, mod); x += 1; x %= mod; return x; } long long int gcd(long long int a, long long int b) { if (a > b)swap(a, b); while (a) { swap(a, b); a %= b; } return b; } public: long long int find(long long int n) { srand(time(NULL)); long long int x = f(rand() % n, n); long long int y = f(f(x, n), n); while (1) { long long int ab = x - y; if (ab < 0)ab = -ab; long long int gc = gcd(ab, n); if (gc == n)return -1LL; if (gc == 1LL) { x = f(x, n); y = f(f(y, n), n); continue; } return gc; } } }; 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 true; } } return false; } 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 (k <= 1000000000000LL) { ok = true; break; } return 1; } } if (ok) { break; } r *= 2LL; } if (ok) { puts("Yes"); } else { puts("No"); } } return 0; }