結果
問題 | No.144 エラトステネスのざる |
ユーザー |
|
提出日時 | 2023-03-07 23:16:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 139 ms / 2,000 ms |
コード長 | 3,812 bytes |
コンパイル時間 | 1,877 ms |
コンパイル使用メモリ | 178,520 KB |
実行使用メモリ | 7,680 KB |
最終ジャッジ日時 | 2024-09-18 02:16:09 |
合計ジャッジ時間 | 3,501 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#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;}