結果

問題 No.2120 場合の数の下8桁
ユーザー 👑 kakel-sankakel-san
提出日時 2023-11-09 23:23:49
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,148 ms / 2,000 ms
コード長 2,785 bytes
コンパイル時間 7,232 ms
コンパイル使用メモリ 158,564 KB
実行使用メモリ 264,488 KB
最終ジャッジ日時 2023-11-09 23:24:15
合計ジャッジ時間 12,322 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
31,268 KB
testcase_01 AC 61 ms
31,524 KB
testcase_02 AC 60 ms
31,524 KB
testcase_03 AC 62 ms
31,524 KB
testcase_04 AC 61 ms
31,524 KB
testcase_05 AC 56 ms
31,268 KB
testcase_06 AC 57 ms
31,268 KB
testcase_07 AC 57 ms
31,268 KB
testcase_08 AC 57 ms
31,268 KB
testcase_09 AC 61 ms
31,524 KB
testcase_10 AC 61 ms
31,524 KB
testcase_11 AC 60 ms
31,524 KB
testcase_12 AC 60 ms
31,524 KB
testcase_13 AC 71 ms
32,548 KB
testcase_14 AC 78 ms
33,188 KB
testcase_15 AC 494 ms
76,892 KB
testcase_16 AC 62 ms
32,036 KB
testcase_17 AC 1,144 ms
115,112 KB
testcase_18 AC 58 ms
31,268 KB
testcase_19 AC 1,148 ms
264,488 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (130 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var m = NN;
        var n = NN;
        WriteLine(Count(m, n));
    }
    static string Count(int m, int n)
    {
        if (m < n)
        {
            return "00000000";
        }
        if (n > m / 2) n = m - n;
        var mod = 100_000_000;
        var ans = 1L;
        var count2 = 0;
        var count5 = 0;
        var dic = new Dictionary<int, int>();
        for (var i = 0; i < n; ++i)
        {
            var ch = m - i;
            while (ch % 2 == 0)
            {
                ++count2;
                ch /= 2;
            }
            while (ch % 5 == 0)
            {
                ++count5;
                ch /= 5;
            }
            ans = ans * ch % mod;
            var pa = i + 1;
            while (pa % 2 == 0)
            {
                --count2;
                pa /= 2;
            }
            while (pa % 5 == 0)
            {
                --count5;
                pa /= 5;
            }
            if (dic.ContainsKey(pa)) ans = ans * dic[pa] % mod;
            else
            {
                var rev = ModInv(pa, mod);
                dic[pa] = rev;
                ans = ans * rev % mod;
            }
        }
        ans = ans * Exp(2, count2, mod) % mod * Exp(5, count5, mod) % mod;
        return ans.ToString("00000000");
    }
    // a ^ -1 mod m を求める
    static int ModInv(int a, int mod)
    {
        (int g, int x, int y) = XGcd(a, mod);
        return (x + mod) % mod;
    }
    // 拡張ユークリッド互除法 ax + by = gcd(a, b) を満たす x, y を求める
    static (int g, int x, int y) XGcd(int a, int b)
    {
        int x0 = 1, y0 = 0, x1 = 0, y1 = 1;
        while (b != 0)
        {
            var q = a / b;
            var prevA = a;
            a = b;
            b = prevA % b;
            var prevX0 = x0;
            var prevY0 = y0;
            x0 = x1;
            x1 = prevX0 - q * x1;
            y0 = y1;
            y1 = prevY0 - q * y1;
        }
        return (a, x0, y0);
    }
    static long Exp(long n, long p, int mod)
    {
        long _n = n % mod;
        var _p = p;
        var result = 1L;
        if ((_p & 1) == 1) result *= _n;
        while (_p > 0)
        {
            _n = _n * _n % mod;
            _p >>= 1;
            if ((_p & 1) == 1) result = result * _n % mod;
        }
        return result;
    }
}
0