結果

問題 No.752 mod数列
ユーザー treeonetreeone
提出日時 2018-11-09 22:54:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,884 bytes
コンパイル時間 2,939 ms
コンパイル使用メモリ 210,084 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-13 12:01:18
合計ジャッジ時間 8,513 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 2 ms
4,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 RE -
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 148 ms
4,380 KB
testcase_16 RE -
testcase_17 AC 167 ms
4,376 KB
testcase_18 AC 156 ms
4,376 KB
testcase_19 AC 158 ms
4,376 KB
testcase_20 AC 167 ms
4,380 KB
testcase_21 AC 167 ms
4,380 KB
testcase_22 RE -
testcase_23 AC 164 ms
4,376 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 137 ms
4,376 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 AC 162 ms
4,376 KB
testcase_30 RE -
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define int long long
#define all(a) a.begin(), a.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;
const int INF = 2e18;
int p, q, MAX;
int d[100010];
vector<PP> a;
vector<int> b;

int calc(int n){
    if(n * n <= p) return d[n];
    int res = MAX;
    int idx = upper_bound(a.begin(), a.end(), PP{n, {INF, INF}}) - a.begin();
    idx--;
    res += b[idx];
    int ed = a[idx].second.first;
    int diff = a[idx].second.second;
    int m = n - a[idx].first + 1;
    int fst = ed - diff * (m - 1);
    int tmp = (fst + ed) * m / 2;
    res += tmp;
    // cout << n << " " << b[idx] << " " << idx << " " << a[idx].first << ' ' << fst << ' ' << ed << ' ' << diff << ' ' << m << ' ' << tmp << endl;
    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> p >> q;
    for(int i = 1; i * i <= p; i++){
        d[i] = d[i - 1] + p % i;
    }
    MAX = *max_element(d, d + 100010);
    int MAXid = max_element(d, d + 100010) - d;
    for(int i = 0; i * i < p; i++){
        int idx = p / (i + 1) + 1;
        int r = p - i * idx;
        a.push_back({idx, {r, i}});
    }
    sort(a.begin(), a.end());
    b.resize(a.size() + 1, 0);
    for(int i = 0; i < a.size() - 1; i++){
        int ed = a[i].second.first;
        int diff = a[i].second.second;
        int fst = ed % diff;
        int n = ed / diff + 1;
        b[i + 1] = b[i] + (fst + ed) * n / 2;
    }
    assert(a[0].first == MAXid + 1);
    // rep(i, 0, p + 1){
    //     cout << calc(i) << endl;
    // }
    rep(i, 0, q){
        int l, r;
        cin >> l >> r;
        int ans = calc(r) - calc(l - 1);
        cout << ans << endl;
    }
}
0