結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー Min_25Min_25
提出日時 2016-10-21 17:23:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,039 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 90,612 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 06:39:01
合計ジャッジ時間 3,214 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 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 L_MAX = 1e9;
  constexpr int sqrt_L = sqrt(L_MAX) + 10;
  static i64 pis[sqrt_L + 1], pil[sqrt_L + 1];
  static i64 psums[sqrt_L + 1], psuml[sqrt_L + 1];
  int N, L;
  while (~scanf("%d %d", &N, &L)) {
    // sublinear
    auto tri = [&](int n) { return i64(n) * (n + 1) / 2; };
    int M = L / (N - 1), v = sqrt(M);
    rep(i, 1, v + 1) pis[i] = i - 1, pil[i] = M / i - 1;
    rep(i, 1, v + 1) psums[i] = tri(i) - 1, psuml[i] = tri(M / i) - 1;
    rep(p, 2, v + 1) if (pis[p] > pis[p - 1]) {
      auto psum = psums[p - 1];
      auto pcnt = pis[p - 1];
      auto q = i64(p) * p;
      rep(i, 1, min(M / q, i64(v)) + 1) {
        i64 d = i64(i) * p;
        pil[i] -= (d <= v ? pil[d] : pis[M / d]) - pcnt;
        psuml[i] -= ((d <= v ? psuml[d] : psums[M / d]) - psum) * p;
      }
      for (int i = v; i >= q; --i) {
        pis[i] -= pis[i / p] - pcnt;
        psums[i] -= (psums[i / p] - psum) * p;
      }
    }
    if (M <= 1) psuml[1] = pil[1] = 0;
    i64 ans = pil[1] * (L + 1) - (N - 1) * psuml[1];
    printf("%lld\n", ans);
  }
}

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