結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー izuru_matsuura
提出日時 2016-11-08 17:50:26
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 150,236 KB
実行使用メモリ 12,076 KB
最終ジャッジ日時 2024-06-12 04:58:35
合計ジャッジ時間 7,128 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void log(A...)(A arg) {
    stderr.writeln(arg);
}
int size(T)(in T s) {
    return cast(int)s.length;
}

const long mod = cast(long)(1e9 + 7);

long extgcd(long a, long b, ref long x, ref long y) {
    long g = a; x = 1; y = 0;
    if (b != 0) {
        g = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
    return g;
}
long inverse(long mod, long v) {
    long x, y;
    if (extgcd(v, mod, x, y) == 1) return (x + mod) % mod;
    return 0;
}

void main() {
    int M; readf("%d\n", &M);
    auto H = readln.chomp.split(" ").map!(to!int).array;

    int k = H.size;

    if (k == 1 && H[0] == 0) {
        writeln(1);
        return;
    }

    int Y = M - (k - 1);
    foreach (h; H) {
        Y -= h;
    }

    if (Y < 0) {
        writeln("NA");
        return;
    }

    auto inv = delegate(long x) => inverse(mod, x);
    long ans = 1;
    for (long i = 1; i <= Y + k; i++) {
        ans = (ans * i) % mod;
    }
    for (long i = 1; i <= k; i++) {
        ans = (ans * inv(i)) % mod;
    }
    for (long i = 1; i <= Y; i++) {
        ans = (ans * inv(i)) % mod;
    }
    writeln(ans);

}
0