結果

問題 No.752 mod数列
ユーザー nanaenanae
提出日時 2018-12-03 13:44:07
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 2,241 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 100,752 KB
実行使用メモリ 23,244 KB
最終ジャッジ日時 2023-09-03 21:20:06
合計ジャッジ時間 5,430 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 2 ms
5,156 KB
testcase_03 AC 3 ms
4,508 KB
testcase_04 AC 2 ms
4,892 KB
testcase_05 AC 3 ms
4,836 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 4 ms
4,384 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 4 ms
4,836 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 92 ms
6,188 KB
testcase_16 AC 129 ms
9,780 KB
testcase_17 AC 157 ms
19,928 KB
testcase_18 AC 133 ms
10,356 KB
testcase_19 AC 153 ms
11,160 KB
testcase_20 AC 189 ms
22,912 KB
testcase_21 AC 168 ms
21,408 KB
testcase_22 AC 166 ms
21,904 KB
testcase_23 AC 170 ms
22,652 KB
testcase_24 AC 169 ms
21,080 KB
testcase_25 AC 46 ms
7,468 KB
testcase_26 AC 126 ms
20,868 KB
testcase_27 AC 86 ms
10,036 KB
testcase_28 AC 119 ms
20,388 KB
testcase_29 AC 134 ms
11,156 KB
testcase_30 AC 168 ms
23,244 KB
testcase_31 AC 2 ms
4,388 KB
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 3 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

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 l = new int[](q);
    auto r = new int[](q);

    int[] bs;

    foreach (i ; 0 .. q) {
        scan(l[i], r[i]);
        bs ~= l[i] - 1;
        bs ~= r[i];
    }

    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]);
    }

    bs.sort();
    long[int] cs;

    int d = p / kmax;

    while (!bs.empty && bs.front <= d) {
        cs[bs.front] = rw[bs.front];
        bs.popFront();
    }

    long res = rw[d];

    foreach_reverse (k ; 0 .. kmax) {
        int s = p / (k + 1);
        int t = k == 0 ? 10^^9 + 7 : (p + k - 1) / k;

        while (!bs.empty && bs.front < t) {
            long rest = res;
            int let = max(0, bs.front + 1 - s - 1);
            rest += 1L * p * let;
            rest -= 1L * k * ksum(s + 1, bs.front + 1);
            cs[bs.front] = rest;
            bs.popFront();
        }

        if (bs.empty) {
            break;
        }

        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);
    }

    foreach (i ; 0 .. q) {
        long ans = cs[r[i]] - cs[l[i]-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