結果

問題 No.1629 Sorting Integers (SUM of M)
ユーザー nebukuro09nebukuro09
提出日時 2021-07-30 20:50:34
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 103,376 KB
実行使用メモリ 5,124 KB
最終ジャッジ日時 2023-09-04 13:30:30
合計ジャッジ時間 2,225 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 3 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 4 ms
4,796 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,384 KB
testcase_16 AC 4 ms
4,772 KB
testcase_17 AC 5 ms
5,124 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;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;

    immutable long MOD = 10^^9 + 7;

    auto P = new long[](N+5);
    P[0] = P[1] = 1;
    foreach (i; 1..N+4) {
        P[i+1] = P[i] * (i + 1) % MOD;
    }

    long ans = 0;
    long base = 0;
    long tmp = 1;
    foreach (i; 0..N) {
        (base += tmp) %= MOD;
        tmp = tmp * 10 % MOD;
    }

    foreach (i, a; A) {
        long n = (i + 1).to!long;
        long c = P[N-1];
        if (A[i] == 0) continue;
        foreach (j; 0..9) {
            if (j == i) {
                c = c * powmod(P[A[j]-1], MOD-2, MOD) % MOD;
            } else {
                c = c * powmod(P[A[j]], MOD-2, MOD) % MOD;
            }
        }
        (ans += base * n % MOD * c % MOD) %= MOD;
    }

    ans.writeln;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0