結果

問題 No.752 mod数列
ユーザー Pachicobue
提出日時 2018-11-10 00:06:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,177 bytes
コンパイル時間 2,208 ms
コンパイル使用メモリ 193,916 KB
最終ジャッジ日時 2025-01-06 16:18:35
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

//=================================
// Created on: 2018/11/09 21:30:39
//=================================
#include <bits/stdc++.h>
using ll = long long;
constexpr ll MAX = 10000000;
constexpr int NUM = 100;
ll small[MAX + 1], start[NUM], end[NUM];
int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    ll P;
    int Q;
    std::cin >> P >> Q;
    for (ll i = 1; i <= MAX; i++) { small[i] = small[i - 1] + (P % i); }
    for (ll i = NUM - 1; i >= 1; i--) { start[i] = P / (i + 1) + 1, end[i] = P / i; }
    auto get = [&](ll ind) {
        if (ind <= MAX) { return small[ind]; }
        ll ans = small[MAX];
        if (ind > P) { ans += (ind - P) * P, ind = P; }
        for (int i = NUM - 1; i >= 1 and start[i] <= ind; i--) {
            if (start[i] > end[i]) { continue; }
            const ll S = std::max(MAX + 1, start[i]), T = std::min(end[i], ind);
            if (S > T) { continue; }
            ans += (2 * P - (P / S) * (S + T)) * (T - S + 1) / 2;
        }
        return ans;
    };
    for (int q = 0; q < Q; q++) {
        ll L, R;
        std::cin >> L >> R;
        std::cout << get(R) - get(L - 1) << std::endl;
    }
    return 0;
}
0