結果

問題 No.752 mod数列
ユーザー betrue12betrue12
提出日時 2018-11-09 22:25:05
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 2,692 ms
使用メモリ 4,284 KB
最終ジャッジ日時 2022-12-26 20:19:52
合計ジャッジ時間 8,029 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
3,528 KB
testcase_01 AC 1 ms
3,500 KB
testcase_02 AC 1 ms
3,544 KB
testcase_03 AC 1 ms
3,456 KB
testcase_04 AC 2 ms
3,384 KB
testcase_05 AC 1 ms
3,564 KB
testcase_06 AC 1 ms
3,400 KB
testcase_07 AC 2 ms
3,564 KB
testcase_08 AC 1 ms
3,528 KB
testcase_09 AC 1 ms
3,560 KB
testcase_10 AC 4 ms
3,968 KB
testcase_11 AC 4 ms
3,676 KB
testcase_12 AC 4 ms
3,884 KB
testcase_13 AC 4 ms
3,860 KB
testcase_14 AC 5 ms
4,032 KB
testcase_15 AC 173 ms
3,560 KB
testcase_16 AC 182 ms
4,224 KB
testcase_17 AC 200 ms
4,284 KB
testcase_18 AC 195 ms
3,556 KB
testcase_19 AC 195 ms
3,588 KB
testcase_20 AC 196 ms
3,560 KB
testcase_21 AC 200 ms
3,440 KB
testcase_22 AC 199 ms
3,572 KB
testcase_23 AC 202 ms
3,572 KB
testcase_24 AC 207 ms
3,432 KB
testcase_25 AC 60 ms
4,080 KB
testcase_26 AC 156 ms
4,092 KB
testcase_27 AC 110 ms
4,020 KB
testcase_28 AC 144 ms
4,120 KB
testcase_29 AC 191 ms
3,864 KB
testcase_30 AC 213 ms
3,924 KB
testcase_31 AC 1 ms
3,536 KB
testcase_32 AC 2 ms
3,388 KB
testcase_33 AC 2 ms
3,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int64_t r[100001], s[100001], mn[100001];

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

    int64_t bound = 1;
    while(bound*bound <= P) bound++;

    r[0] = 0;
    for(int i=1; i<bound; i++){
        r[i] = r[i-1] + P%i;
    }

    s[0] = 0;
    for(int i=0; i<=P/bound; i++){
        mn[i] = P/(i+1) + 1;
        if(i>0){
            int64_t num = mn[i-1] - mn[i];
            s[i] = s[i-1] + P*num - (mn[i-1]-1 + mn[i]) * num / 2 * i;
        }
    }

    while(Q--){
        int64_t Lo, Ro;
        cin >> Lo >> Ro;
        int64_t ans = 0;
        if(Lo <= bound-1){
            int L = Lo, R = min(bound-1, Ro);
            ans += r[R] - r[L-1];
        }
        if(bound <= Ro){
            int L = max(Lo, bound), R = Ro;
            int lq = P/L, rq = P/R;
            if(lq == rq){
                int64_t num = R - L + 1;
                ans += P*num - (R+L) * num / 2 * lq;
            }else{
                ans += s[lq-1] - s[rq];
                int64_t num = R - mn[rq] + 1;
                ans += P*num - (R+mn[rq]) * num / 2 * rq;
                num = mn[lq-1] - L;
                ans += P*num - (mn[lq-1]-1+L) * num / 2 * lq;
            }
        }
        cout << ans << endl;
    }


    return 0;
}
0