結果

問題 No.752 mod数列
ユーザー nanaenanae
提出日時 2018-12-03 11:48:05
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,897 bytes
コンパイル時間 597 ms
コンパイル使用メモリ 95,124 KB
実行使用メモリ 8,496 KB
最終ジャッジ日時 2023-09-03 21:18:34
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum lim = 10^^9;
enum kmax = 10^^4;

void main() {
    int p, q;
    scan(p, q);

    auto rw = new long[](10^^5 + 1);
    foreach (i ; 1 .. 10^^5 + 1) {
        rw[i] = rw[i-1] + (p % i);
    }

    debug {
        //writeln(rw[0..30]);
    }

    long mrw(int r) {
        if (r <= 10^^5) {
            return rw[r];
        }

        long res;
        int am = 10^^9 + 7;

        foreach_reverse (k ; 1 .. kmax) {
            int s = p / (k + 1);
            int t = (p + k - 1) / k;

            am = min(am, s);

            if (r < t) {
                t = (r == t - 1 ? t : r + 1);
            }


            int le = t - s - 1;
            if (le <= 0) continue;

            debug {
                //writefln("%s, %s", s, t);
            }
            
            res += 1L * p * le;
            res -= 1L * k * ksum(s + 1, t);
        }

        res += rw[am];

        return res;
    }

    while (q--) {
        int li, ri;
        scan(li, ri);
        long ans = mrw(ri) - mrw(li-1);
        writeln(ans);
    }
}


// sum_{i = l}^{r - 1} k
long ksum(int l, int r) {
    if (l >= r) {
        return 0;
    }
    return 1L * r * (r - 1) / 2 - 1L * l * (l - 1) / 2;
}






















void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0