結果

問題 No.752 mod数列
ユーザー betrue12betrue12
提出日時 2018-11-09 22:25:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 198,756 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-13 11:45:08
合計ジャッジ時間 7,733 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,384 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 171 ms
4,384 KB
testcase_16 AC 187 ms
4,376 KB
testcase_17 AC 199 ms
4,380 KB
testcase_18 AC 178 ms
4,376 KB
testcase_19 AC 193 ms
4,380 KB
testcase_20 AC 200 ms
4,376 KB
testcase_21 AC 198 ms
4,376 KB
testcase_22 AC 202 ms
4,380 KB
testcase_23 AC 208 ms
4,376 KB
testcase_24 AC 203 ms
4,384 KB
testcase_25 AC 61 ms
4,380 KB
testcase_26 AC 160 ms
4,380 KB
testcase_27 AC 115 ms
4,376 KB
testcase_28 AC 147 ms
4,380 KB
testcase_29 AC 191 ms
4,380 KB
testcase_30 AC 217 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 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