結果

問題 No.752 mod数列
ユーザー merom686
提出日時 2018-11-16 16:50:34
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,047 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 76,912 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-24 14:10:23
合計ジャッジ時間 3,318 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int p, q;
    cin >> p >> q;

    auto g = [&](int l, int r) {
        int l1 = p % (l + 1);
        int r1 = p % r;
        return (ll)(l1 + r1) * (r - l) / 2;
    };

    int y = (int)sqrt(p);
    int x = p / y;
    vector<ll> s(x), t(y + 1);
    for (int i = 1; i < x; i++) {
        s[i] = s[i - 1] + p % i;
    }
    for (int i = 1; i <= y; i++) {
        t[i] = t[i - 1] + g(p / (i + 1), p / i);
    }

    auto f = [&](ll a) {
        if (a < x) return s[a];
        ll r = s[x - 1];
        if (p < a) {
            r += (a - p) * p;
            a = p;
        }
        int i = p / a;
        r += t[y] - t[i] + g(p / (i + 1), a);
        return r;
    };

    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;

        cout << f(r) - f(l - 1) << '\n';
    }

    return 0;
}
0