結果

問題 No.752 mod数列
ユーザー ei1333333ei1333333
提出日時 2018-11-09 22:47:29
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 1,467 bytes
コンパイル時間 1,917 ms
使用メモリ 5,328 KB
最終ジャッジ日時 2022-12-26 20:36:48
合計ジャッジ時間 7,909 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
3,516 KB
testcase_01 AC 2 ms
3,472 KB
testcase_02 AC 2 ms
3,528 KB
testcase_03 AC 2 ms
3,540 KB
testcase_04 AC 2 ms
3,516 KB
testcase_05 AC 2 ms
3,628 KB
testcase_06 AC 2 ms
3,456 KB
testcase_07 AC 2 ms
3,532 KB
testcase_08 AC 2 ms
3,588 KB
testcase_09 AC 2 ms
3,568 KB
testcase_10 AC 22 ms
4,996 KB
testcase_11 AC 11 ms
4,076 KB
testcase_12 AC 18 ms
4,984 KB
testcase_13 AC 14 ms
4,076 KB
testcase_14 AC 22 ms
4,984 KB
testcase_15 AC 187 ms
3,424 KB
testcase_16 AC 244 ms
5,328 KB
testcase_17 AC 251 ms
5,248 KB
testcase_18 AC 190 ms
3,472 KB
testcase_19 AC 218 ms
3,788 KB
testcase_20 AC 208 ms
3,668 KB
testcase_21 AC 206 ms
3,664 KB
testcase_22 AC 206 ms
3,720 KB
testcase_23 AC 215 ms
3,748 KB
testcase_24 AC 215 ms
3,636 KB
testcase_25 AC 85 ms
5,260 KB
testcase_26 AC 190 ms
5,320 KB
testcase_27 AC 139 ms
5,056 KB
testcase_28 AC 178 ms
5,320 KB
testcase_29 AC 211 ms
4,992 KB
testcase_30 AC 232 ms
5,128 KB
testcase_31 AC 1 ms
3,444 KB
testcase_32 AC 2 ms
3,384 KB
testcase_33 AC 16 ms
4,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;


int main() {
  int P, Q;
  cin >> P >> Q;

  vector< pair< pair< int, int >, int > > rad;
  int ptr = 1;
  while(ptr <= P) {
    int ok = ptr, ng = P + 1;
    int real = P / ptr;
    while(ng - ok > 1) {
      int mid = (ok + ng) / 2;
      if(P / mid == real) ok = mid;
      else ng = mid;
    }
    rad.emplace_back(make_pair(ptr, ok), P / ptr);
    ptr = ok + 1;
  }

  vector< __int128_t > sum;
  sum.push_back(0);
  __int128_t add = 0LL;
  for(auto &p : rad) {
    auto range = p.first;
    auto val = p.second;
    int latte = range.first;
    int malta = range.second;
    add += val * (1LL * latte * (latte - 1) / 2);
    add -= val * (1LL * malta * (malta + 1) / 2);
    sum.push_back(add);
  }

  auto get = [&](int L) {
    __int128_t add = 1LL * P * L;
    int it1 = lower_bound(begin(rad), end(rad), make_pair(make_pair(L - 1, -1), L)) - begin(rad);
    --it1;
    --it1;
    it1 = max(it1, 0);
    add += sum[it1];
    for(int i = it1; i < rad.size(); i++) {
      auto &p = rad[i];
      auto range = p.first;
      auto val = p.second;
      int latte = range.first;
      int malta = min(range.second, L);
      if(latte > malta) break;
      add += val * (1LL * latte * (latte - 1) / 2);
      add -= val * (1LL * malta * (malta + 1) / 2);
    }
    return (int64) add;
  };

  while(Q--) {
    int L, R;
    cin >> L >> R;
    cout << get(R) - get(L - 1) << endl;
  }

}

0