結果

問題 No.144 エラトステネスのざる
ユーザー dekomori_sanaedekomori_sanae
提出日時 2022-02-19 13:25:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 872 ms
コンパイル使用メモリ 90,288 KB
実行使用メモリ 26,664 KB
最終ジャッジ日時 2023-09-11 20:37:54
合計ジャッジ時間 5,474 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 488 ms
26,560 KB
testcase_14 AC 495 ms
26,628 KB
testcase_15 AC 498 ms
26,560 KB
testcase_16 AC 499 ms
26,548 KB
testcase_17 AC 493 ms
26,664 KB
testcase_18 AC 500 ms
26,492 KB
testcase_19 AC 463 ms
26,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;
const ll INF = 1e16;

struct Eratosthenes {

    vl isprime;  // isprime[i] : iが素数なら1
    vl minfactor;  // minfactor[i] : iを割り切る最小の素数
    vl mobius;  // mobius[i] : メビウス関数 μ(i)

    Eratosthenes(ll n) : isprime(n+1, 1), 
                         minfactor(n+1, -1), 
                         mobius(n+1, 1) {
        isprime[1] = 0;
        minfactor[1] = 1;
        exrep(p, 2, n) {
            if(!isprime[p]) {
                continue;
            }
            minfactor[p] = p;
            mobius[p] = -1;
            for(ll q = 2*p; q <= n; q += p) {
                isprime[q] = 0;
                if(minfactor[q] == -1) {
                    minfactor[q] = p;
                }
                if((q / p) % p == 0) {
                    mobius[q] = 0;
                }
                else {
                    mobius[q] = -mobius[q];
                }
            }
        }
    }

    // 高速素因数分解。O(log(n))でnを素因数分解する
    vector<P> factorize(ll n) {  // (素因数, 指数) のvectorを返す
        vector<P> res;
        while(n > 1) {
            ll p = minfactor[n];
            ll i = 0;
            while(minfactor[n] == p) {
                n /= p;
                i++;
            }
            res.emplace_back(make_pair(p, i));
        }   
        return res;
    }

    // 高速約数列挙。O(σ(n))でnの約数を求める。σ(n)はnの約数の個数
    vl divisors(ll n) {
        vl res({1});
        auto pf = factorize(n);
        for(auto p : pf) {
            ll s = res.size();
            rep(i, s) {
                ll x = 1;
                rep(j, p.second) {
                    x *= p.first;
                    res.pb(res[i] * x);
                }
            }
        }
        return res;
    }
};

int main() {
    ll n;
    double p;
    cin >> n >> p;

    Eratosthenes e(n+1);

    double ans = 0.0;
    exrep(i, 2, n) {
        vl v = e.divisors(i);
        int m = v.size() - 2;
        if(m == 0) {
            ans += 1.0;
        }
        else {
            ans += pow(1.0 - p, m);
        }
    }

    exout(ans);
    re0;
}
0