結果

問題 No.2120 場合の数の下8桁
ユーザー 👑 chro_96chro_96
提出日時 2022-11-04 22:39:53
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 1,844 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 30,568 KB
実行使用メモリ 40,972 KB
最終ジャッジ日時 2023-09-26 01:09:08
合計ジャッジ時間 2,200 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
40,972 KB
testcase_01 AC 12 ms
40,940 KB
testcase_02 AC 12 ms
40,828 KB
testcase_03 AC 12 ms
40,760 KB
testcase_04 AC 11 ms
40,928 KB
testcase_05 AC 11 ms
40,920 KB
testcase_06 AC 12 ms
40,924 KB
testcase_07 AC 12 ms
40,876 KB
testcase_08 AC 11 ms
40,796 KB
testcase_09 AC 12 ms
40,916 KB
testcase_10 AC 11 ms
40,940 KB
testcase_11 AC 11 ms
40,944 KB
testcase_12 AC 11 ms
40,892 KB
testcase_13 AC 13 ms
40,836 KB
testcase_14 AC 14 ms
40,888 KB
testcase_15 AC 102 ms
40,828 KB
testcase_16 AC 12 ms
40,832 KB
testcase_17 AC 291 ms
40,892 KB
testcase_18 AC 11 ms
40,692 KB
testcase_19 AC 292 ms
40,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

long long gcd (long long a, long long b, long long *x, long long *y) {
  long long ans = 0LL;
  
  if (a <= 0LL || b <= 0LL) {
    return 0LL;
  }
  
  if (a%b == 0LL) {
    *x = 0LL;
    *y = 1LL;
    return b;
  }
  
  ans = gcd(b, a%b, y, x);
  *y -= (a/b)*(*x);
  return ans;
}

int main () {
  int m = 0;
  int n = 0;
  
  int res = 0;
  
  long long ans = 1LL;
  long long inv[5000001] = {};
  
  int cnt2 = 0;
  int cnt5 = 0;
  
  int pow2 = 2;
  int pow5 = 5;
  
  long long mod_num = 100000000LL;
  
  res = scanf("%d", &m);
  res = scanf("%d", &n);
  
  if (m < n) {
    printf("00000000\n");
    return 0;
  }
  
  if (m-n < n) {
    n = m-n;
  }
  
  while (pow2 <= m) {
    cnt2 += m/pow2-(m-n)/pow2;
    cnt2 -= n/pow2;
    pow2 *= 2;
  }
  
  while (pow5 <= m) {
    cnt5 += m/pow5-(m-n)/pow5;
    cnt5 -= n/pow5;
    pow5 *= 5;
  }
  
  inv[1] = 1LL;
  for (int i = 2; i <= n; i++) {
    if (inv[i] <= 0LL) {
      if (i%2LL == 0LL) {
        inv[i] = inv[i/2LL];
      } else if (i%5LL == 0LL) {
        inv[i] = inv[i/5LL];
      } else {
        long long x = 0LL;
        long long y = 0LL;
        long long d = gcd(mod_num, (long long)i, &x, &y);
        if (y <= 0LL) {
          y += (1+(-y)/mod_num)*mod_num;
        }
        inv[i] = y%mod_num;
      }
      for (int p = 2; i*p <= n; p++) {
        inv[p*i] = (inv[p]*inv[i])%mod_num;
      }
    }
  }
  
  for (int i = 0; i < n; i++) {
    long long tmp = (long long) (m-i);
    while (tmp%2LL == 0LL) {
      tmp /= 2LL;
    }
    while (tmp%5LL == 0LL) {
      tmp /= 5LL;
    }
    ans *= tmp;
    ans %= mod_num;
    ans *= inv[i+1];
    ans %= mod_num;
  }
  
  while (cnt2 > 0) {
    ans = (ans*2LL)%mod_num;
    cnt2--;
  }
  
  while (cnt5 > 0) {
    ans = (ans*5LL)%mod_num;
    cnt5--;
  }
  
  printf("%08lld\n", ans);
  return 0;
}
0