結果
| 問題 |
No.2795 Perfect Number
|
| コンテスト | |
| ユーザー |
ManjushriMitra
|
| 提出日時 | 2025-03-20 15:24:31 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 2,000 ms |
| コード長 | 4,325 bytes |
| コンパイル時間 | 3,731 ms |
| コンパイル使用メモリ | 289,272 KB |
| 実行使用メモリ | 7,328 KB |
| 最終ジャッジ日時 | 2025-03-20 15:24:37 |
| 合計ジャッジ時間 | 5,188 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,m,n) for(int i=m; i<int(n); ++i)
#define repll(i,m,n) for(ll i=m; i<ll(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define leng(a) int(a.size())
#define fi first
#define se second
template<typename T> bool chmin(T& a, T b){ if(a > b){a = b; return true;} return false; }
template<typename T> bool chmax(T& a, T b){ if(a < b){a = b; return true;} return false; }
template<typename T> T gcd(T a, T b){ return a % b ? gcd(b, a % b) : b; }
template<typename T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
// Pollard’s Rho の平均計算量: O(n^(1/4))
// Miller-Rabin 素数判定 の計算量: O(k*(log(n))^3) (kは試行回数)
// 64bitの安全な (a * b) % m 計算(乗算分割法)
ll mod_mul(ll a, ll b, ll m) {
ll res = 0;
while (b) {
if (b & 1) res = (res + a) % m;
a = (a + a) % m;
b >>= 1;
}
return res;
}
// 繰り返し二乗法による (a^b) % m 計算
ll mod_pow(ll a, ll b, ll m) {
ll res = 1;
while (b) {
if (b & 1) res = mod_mul(res, a, m);
a = mod_mul(a, a, m);
b >>= 1;
}
return res;
}
// Miller-Rabin素数判定(確率的)
bool is_prime(ll n) {
if (n < 2) return false;
if (n % 2 == 0) return (n == 2);
ll d = n - 1;
int s = 0;
while (d % 2 == 0) d /= 2, ++s;
vector<ll> test_bases = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; // 試行基底
for (ll a : test_bases) {
if (a >= n) break;
ll x = mod_pow(a, d, n);
if (x == 1 || x == n - 1) continue;
bool composite = true;
for (int r = 0; r < s - 1; ++r) {
x = mod_mul(x, x, n);
if (x == n - 1) {
composite = false;
break;
}
}
if (composite) return false;
}
return true;
}
// Pollard's Rho 法による因数探索
ll pollard_rho(ll n) {
if (n % 2 == 0) return 2;
static mt19937_64 rng(random_device{}()); // 乱数生成
uniform_int_distribution<ll> dist(1, n - 1);
ll x = dist(rng), y = x, c = dist(rng);
auto f = [&](ll v) { return (mod_mul(v, v, n) + c) % n; };
while (true) {
x = f(x);
y = f(f(y));
ll d = gcd(abs(x - y), n);
if (d > 1 && d < n) return d;
if (d == n) return pollard_rho(n);
}
}
// 素因数分解
void factorize(ll n, vector<ll>& factors) {
if (n == 1) return;
if (is_prime(n)) {
factors.push_back(n);
return;
}
ll divisor = pollard_rho(n);
factorize(divisor, factors);
factorize(n / divisor, factors);
}
// 素因数列挙
vector<ll> prime_factorization(ll n) {
vector<ll> factors;
while (n % 2 == 0) { // 偶数処理
factors.push_back(2);
n /= 2;
}
factorize(n, factors);
sort(factors.begin(), factors.end());
return factors;
}
// 素因数列挙(pair)
vector<pair<ll, ll>> prime_factorization2(ll n) {
vector<ll> pf = prime_factorization(n);
vector<pair<ll, ll>> factors;
ll ex = 1LL;
for(int i = 1; i < int(pf.size()); ++i){
if(pf[i] != pf[i-1]){
factors.push_back({pf[i-1], ex});
ex = 1LL;
}else{
++ex;
}
}
factors.push_back({pf.back(), ex});
return factors;
}
// 約数列挙
vector<ll> divisor_list(ll n) {
vector<ll> divisors({1LL});
auto pf = prime_factorization2(n);
for(auto p : pf){
int siz = divisors.size();
for(int i = 0; i < siz; ++i){
ll v = 1LL;
for(int j = 0; j < int(p.second); ++j){
v *= p.first;
divisors.push_back(divisors[i] * v);
}
}
}
// sort(divisors.begin(), divisors.end());
return divisors;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
if(N == 1LL){
cout << "No" << endl;
return 0;
}
auto dlist = divisor_list(N);
ll sum = 0LL;
for(ll d : dlist){
if(sum > 2LL*N - d){
cout << "No" << endl;
return 0;
}
sum += d;
}
cout << (sum == 2LL*N ? "Yes" : "No") << endl;
return 0;
}
ManjushriMitra