結果

問題 No.752 mod数列
ユーザー poyompoypoyompoy
提出日時 2018-11-09 22:06:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 79,992 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 04:50:55
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 6 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 5 ms
6,944 KB
testcase_14 AC 6 ms
6,940 KB
testcase_15 AC 152 ms
6,940 KB
testcase_16 AC 170 ms
6,944 KB
testcase_17 AC 168 ms
6,940 KB
testcase_18 AC 151 ms
6,944 KB
testcase_19 AC 159 ms
6,940 KB
testcase_20 AC 149 ms
6,944 KB
testcase_21 AC 154 ms
6,940 KB
testcase_22 AC 155 ms
6,940 KB
testcase_23 AC 151 ms
6,944 KB
testcase_24 AC 151 ms
6,944 KB
testcase_25 AC 48 ms
6,940 KB
testcase_26 AC 130 ms
6,940 KB
testcase_27 AC 91 ms
6,944 KB
testcase_28 AC 118 ms
6,940 KB
testcase_29 AC 161 ms
6,940 KB
testcase_30 AC 156 ms
6,940 KB
testcase_31 AC 1 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct P {
  long long l;
  long long r;
  long long sum;
  P(long long l_, long long r_, long long sum_) : l(l_), r(r_), sum(sum_) {}
};

bool operator<(P a, P b) {
  return a.l < b.l;
}

long long S(long long l, long long r) {
  return r * (r + 1) / 2 - l * (l + 1) / 2;
}

vector<P> kinoe(long long n) {
  vector<P> res;
  int i;
  for (i = 1; i * i <= n; i++) {
    res.emplace_back(n / (i + 1) + 1, n / i + 1, i * S(n / (i + 1), n / i));
  }
  for (long long j = 1; j <= n / i; j++) {
    res.emplace_back(j, j + 1, (n / j) * j);
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {
  long long p;
  int q;
  cin >> p >> q;
  auto ps = kinoe(p);
  vector<long long> sum(ps.size() + 1);
  for (int i = 0; i < ps.size(); i++) {
    sum[i + 1] = sum[i] + ps[i].sum;
  }
  auto f = [&](long long x) {
    if (x == 0) return 0LL;
    long long res = 0;
    if (x > p) {
      res += (x - p) * p;
      x = p;
    }
    int k = (upper_bound(ps.begin(), ps.end(), P(x, 0, 0)) - ps.begin()) - 1;
    res += p*x - sum[k];
    res -= (p / x) * S(ps[k].l - 1, x);
    return res;
  };
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    cout << f(r) - f(l - 1) << endl;
  }
}
0