結果

問題 No.144 エラトステネスのざる
ユーザー Min_25Min_25
提出日時 2017-01-07 08:32:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,378 bytes
コンパイル時間 2,370 ms
コンパイル使用メモリ 92,184 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 15:41:03
合計ジャッジ時間 2,859 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i8 = signed char;
using i16 = signed short;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

void solve() {
  const int N_MAX = 1e9;
  constexpr int sqrt_N = sqrt(N_MAX);
  static int smalls[sqrt_N + 1], larges[sqrt_N + 1];
  static int primes[sqrt_N + 1];

  int N;
  while (~scanf("%d", &N)) {
    double p; scanf("%lf", &p);
    int v = sqrt(N);
    rep(i, 1, v + 1) smalls[i] = i - 1, larges[i] = N / i - 1;
    rep(p, 2, v + 1) if (smalls[p] > smalls[p - 1]) {
      int q = p * p, pcnt = smalls[p - 1];
      rep(i, 1, min(N / q, v) + 1) {
        int d = i * p;
        larges[i] -= ((d <= v) ? larges[d] : smalls[N / d]) - pcnt;
      }
      for (int i = v; i >= q; --i) smalls[i] -= smalls[i / p] - pcnt;
    }
    int prime_size = 0;
    rep(i, 2, v + 1) if (smalls[i] > smalls[i - 1]) {
      primes[prime_size++] = i;
    }
    primes[prime_size++] = v + 1;

    static double pows[2000] = {1};
    rep(i, 1, 2000) pows[i] = pows[i - 1] * (1 - p);

    function< double(int, int, int) > rec = [&] (int n, int pi, int d) {
      double ret = 0.0;
      int t = ((n > v) ? larges[N / n] : smalls[n]) - pi;
      ret += pows[2 * (d - 1)] * t;
      rep(pj, pi, prime_size) {
        int pr = primes[pj];
        if (pr * pr > n) break;
        for (int q = pr, nd = d * 2; i64(q) * pr <= n; q *= pr) {
          ret += rec(n / q, pj + 1, nd);
          nd += d;
          ret += pows[nd - 2];
        }
      }
      return ret;
    };
    double ans = rec(N, 0, 1);
    printf("%.12f\n", ans);
  }
}

int main() {
  auto beg = clock();
  solve();
  auto end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
}
0