結果

問題 No.752 mod数列
ユーザー nebukuro09nebukuro09
提出日時 2018-11-09 22:11:02
言語 D
(dmd 2.102.2)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,071 bytes
コンパイル時間 1,957 ms
実行使用メモリ 15,208 KB
最終ジャッジ日時 2022-12-26 20:07:36
合計ジャッジ時間 7,203 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
3,304 KB
testcase_01 AC 2 ms
3,308 KB
testcase_02 AC 2 ms
3,344 KB
testcase_03 AC 2 ms
3,232 KB
testcase_04 AC 1 ms
3,300 KB
testcase_05 AC 3 ms
3,296 KB
testcase_06 AC 3 ms
3,348 KB
testcase_07 AC 2 ms
3,232 KB
testcase_08 AC 3 ms
3,280 KB
testcase_09 AC 2 ms
3,324 KB
testcase_10 AC 38 ms
6,356 KB
testcase_11 AC 17 ms
4,512 KB
testcase_12 AC 31 ms
5,436 KB
testcase_13 AC 24 ms
5,168 KB
testcase_14 AC 38 ms
6,356 KB
testcase_15 AC 126 ms
4,280 KB
testcase_16 AC 205 ms
7,544 KB
testcase_17 AC 217 ms
15,208 KB
testcase_18 AC 130 ms
4,280 KB
testcase_19 AC 146 ms
4,368 KB
testcase_20 AC 136 ms
4,388 KB
testcase_21 AC 138 ms
5,356 KB
testcase_22 AC 138 ms
8,796 KB
testcase_23 AC 141 ms
4,624 KB
testcase_24 AC 137 ms
4,284 KB
testcase_25 AC 85 ms
7,552 KB
testcase_26 AC 167 ms
7,544 KB
testcase_27 AC 122 ms
7,184 KB
testcase_28 AC 155 ms
7,416 KB
testcase_29 AC 169 ms
6,132 KB
testcase_30 AC 136 ms
6,244 KB
testcase_31 AC 1 ms
3,252 KB
testcase_32 AC 1 ms
3,248 KB
testcase_33 AC 29 ms
5,364 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