結果

問題 No.752 mod数列
ユーザー nebukuro09nebukuro09
提出日時 2018-11-09 22:11:02
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 101,512 KB
実行使用メモリ 11,668 KB
最終ジャッジ日時 2023-09-03 21:15:07
合計ジャッジ時間 7,239 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 38 ms
6,200 KB
testcase_11 AC 17 ms
4,876 KB
testcase_12 AC 30 ms
5,424 KB
testcase_13 AC 25 ms
6,216 KB
testcase_14 AC 39 ms
6,200 KB
testcase_15 AC 125 ms
4,376 KB
testcase_16 AC 208 ms
7,208 KB
testcase_17 AC 217 ms
11,668 KB
testcase_18 AC 131 ms
4,376 KB
testcase_19 AC 147 ms
4,380 KB
testcase_20 AC 138 ms
4,380 KB
testcase_21 AC 138 ms
6,456 KB
testcase_22 AC 138 ms
11,104 KB
testcase_23 AC 141 ms
4,376 KB
testcase_24 AC 137 ms
4,380 KB
testcase_25 AC 84 ms
6,952 KB
testcase_26 AC 167 ms
7,252 KB
testcase_27 AC 123 ms
6,984 KB
testcase_28 AC 157 ms
7,528 KB
testcase_29 AC 170 ms
6,944 KB
testcase_30 AC 133 ms
7,208 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 29 ms
5,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main() {
    auto s = readln.split.map!(to!long);
    auto P = s[0];
    auto Q = s[1];

    long[] A;
    long[] B;

    for (long i = 1; ; ) {
        A ~= i;
        B ~= P / i;
        if (B.back == 0) break;
        long lo = i;
        long hi = P + 10;
        while (hi - lo > 1) {
            long mid = (hi + lo) / 2;
            if (P / mid == P / i) lo = mid;
            else hi = mid;
        }
        i = hi;
    }

    int N = A.length.to!int;
    long[] C = new long[](N-1);
    long[] D = new long[](N);
    foreach (i; 0..N-1) {
        long n = A[i+1] - A[i];
        long a = P % A[i];
        long d = -P / A[i];
        C[i] = n * (2 * a + (n - 1) * d) / 2;
        D[i+1] = D[i] + C[i];
    }

    int bs(long n) {
        int lo = 0;
        int hi = N;
        while (hi - lo > 1) {
            int mid = (hi + lo) / 2;
            if (P / n > B[mid]) hi = mid;
            else lo = mid;
        }
        return lo;
    }

    while (Q--) {
        s = readln.split.map!(to!long);
        auto L = s[0];
        auto R = s[1];
        long ans = 0;
        if (L > P) {
            writeln((R - L + 1) * P);
            continue;
        }
        if (R > P) {
            ans += (R - P) * P;
            R = P;
        }
        int bsL = bs(L);
        int bsR = bs(R);
        long n, a, d;
        if (bsL == bsR) {
            n = R - L + 1;
            a = P % L;
            d = - P / L;
            ans += n * (2 * a + (n - 1) * d) / 2;
            ans.writeln;
            continue;
        }
        if (bsR - bsL >= 2) {
            ans += D[bsR] - D[bsL+1];
        }
        n = A[bsL+1] - L;
        a = P % L;
        d = -P / L;
        ans += n * (2 * a + (n - 1) * d) / 2;
        n = R - A[bsR] + 1;
        a = P % A[bsR];
        d = -P / A[bsR];
        ans += n * (2 * a + (n - 1) * d) / 2;
        ans.writeln;
    }
}
0