結果

問題 No.144 エラトステネスのざる
ユーザー AnchorBluesAnchorBlues
提出日時 2023-03-07 23:16:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 3,812 bytes
コンパイル時間 1,779 ms
コンパイル使用メモリ 179,476 KB
実行使用メモリ 7,544 KB
最終ジャッジ日時 2023-10-18 05:38:13
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,488 KB
testcase_01 AC 17 ms
7,484 KB
testcase_02 AC 17 ms
7,488 KB
testcase_03 AC 17 ms
7,484 KB
testcase_04 AC 18 ms
7,488 KB
testcase_05 AC 17 ms
7,488 KB
testcase_06 AC 18 ms
7,484 KB
testcase_07 AC 17 ms
7,544 KB
testcase_08 AC 18 ms
7,488 KB
testcase_09 AC 18 ms
7,488 KB
testcase_10 AC 17 ms
7,488 KB
testcase_11 AC 18 ms
7,544 KB
testcase_12 AC 17 ms
7,488 KB
testcase_13 AC 127 ms
7,484 KB
testcase_14 AC 130 ms
7,544 KB
testcase_15 AC 129 ms
7,488 KB
testcase_16 AC 131 ms
7,488 KB
testcase_17 AC 130 ms
7,488 KB
testcase_18 AC 131 ms
7,544 KB
testcase_19 AC 154 ms
7,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

const ll INF = 1LL << 60;

template <class T>
void chmax(T& a, T b) {
    if (b > a) a = b;
}
template <class T>
void chmin(T& a, T b) {
    if (b < a) a = b;
}

template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const pair<T, S>& x) noexcept {
    return os << "(" << x.first << ", " << x.second << ")";
}

template <typename T>
void print_vector(vector<T> a) {
    cout << '[';
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i != a.size() - 1) {
            cout << ", ";
        }
    }
    cout << ']' << endl;
}

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll ceilll(ll x, ll y) { return (x + y - 1) / y; }
ll mod(ll x, ll y) { return (x + 10000000) % y; }

// 約数全列挙
ll all_divisors_size(ll K) {
    int cnt = 0;
    for (ll i = 1; i * i <= K; i++) {
        if (K % i != 0) continue;
        cnt++;
        if (i * i != K) cnt++;
    }
    return cnt;
}

// 素因数分解
vector<pair<ll, ll>> prime_factorize(ll N) {
    vector<pair<ll, ll>> res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}

double memo[100000];

double powdouble(double a, ll x) {
    if (x == 0) return 1;
    if (memo[x]) return memo[x];
    memo[x] = powdouble(a, x - 1) * a;
    return memo[x];
}

class Eratosthenes {
   public:
    // コンストラクタ
    Eratosthenes();
    Eratosthenes(int);

    // 高速素因数分解
    vector<pii> factorize(int n) const;
    // 素数判定
    bool is_prime(int n) const;

   private:
    std::vector<bool> _isprime;
    std::vector<int> _minfactor;
};

// コンストラクタ
Eratosthenes::Eratosthenes() {}

Eratosthenes::Eratosthenes(int N) {
    _isprime = std::vector<bool>(N + 1, true);
    _minfactor = std::vector<int>(N + 1, -1);
    // 1 は予めふるい落としておく
    _isprime[1] = false;
    _minfactor[1] = 1;

    // 篩
    for (int p = 2; p <= N; ++p) {
        // すでに合成数であるものはスキップする
        if (!_isprime[p]) continue;

        // p についての情報更新
        _minfactor[p] = p;

        // p 以外の p の倍数から素数ラベルを剥奪
        for (int q = p * 2; q <= N; q += p) {
            // q は合成数なのでふるい落とす
            _isprime[q] = false;

            // q は p で割り切れる旨を更新
            if (_minfactor[q] == -1) _minfactor[q] = p;
        }
    }
}

vector<pii> Eratosthenes::factorize(int n) const {
    vector<pii> res;
    while (n > 1) {
        int p = _minfactor[n];
        int exp = 0;

        // n で割り切れる限り割る
        while (_minfactor[n] == p) {
            n /= p;
            ++exp;
        }
        res.emplace_back(p, exp);
    }
    return res;
}

bool Eratosthenes::is_prime(int n) const { return _isprime[n]; }

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll N;
    cin >> N;
    double p;
    cin >> p;
    double ret = 0;
    auto es = Eratosthenes(1000010);
    for (int i = 2; i <= N; i++) {
        auto tmp = es.factorize(i);
        // print_vector(tmp);
        ll cnt = 1;
        for (auto& v : tmp) {
            cnt *= v.second + 1;
        }
        ret += powdouble(1 - p, cnt - 2);
    }
    std::cout << fixed << setprecision(12) << ret << "\n";
    return 0;
}
0