結果

問題 No.802 だいたい等差数列
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2019-09-21 00:23:57
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 30,276 KB
実行使用メモリ 13,628 KB
最終ジャッジ日時 2023-10-13 09:14:53
合計ジャッジ時間 2,258 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
13,432 KB
testcase_01 AC 21 ms
13,548 KB
testcase_02 AC 22 ms
13,440 KB
testcase_03 AC 22 ms
13,552 KB
testcase_04 AC 22 ms
13,556 KB
testcase_05 AC 21 ms
13,400 KB
testcase_06 AC 22 ms
13,552 KB
testcase_07 AC 22 ms
13,532 KB
testcase_08 AC 22 ms
13,576 KB
testcase_09 AC 21 ms
13,548 KB
testcase_10 AC 22 ms
13,564 KB
testcase_11 AC 22 ms
13,544 KB
testcase_12 AC 22 ms
13,520 KB
testcase_13 AC 22 ms
13,612 KB
testcase_14 AC 22 ms
13,576 KB
testcase_15 AC 23 ms
13,540 KB
testcase_16 AC 22 ms
13,620 KB
testcase_17 AC 22 ms
13,476 KB
testcase_18 AC 22 ms
13,396 KB
testcase_19 AC 22 ms
13,628 KB
testcase_20 AC 24 ms
13,552 KB
testcase_21 AC 25 ms
13,528 KB
testcase_22 AC 21 ms
13,436 KB
testcase_23 AC 23 ms
13,588 KB
testcase_24 AC 21 ms
13,472 KB
testcase_25 AC 21 ms
13,540 KB
testcase_26 AC 21 ms
13,612 KB
testcase_27 AC 21 ms
13,564 KB
testcase_28 AC 23 ms
13,524 KB
testcase_29 AC 24 ms
13,544 KB
testcase_30 AC 22 ms
13,560 KB
testcase_31 AC 22 ms
13,620 KB
testcase_32 AC 22 ms
13,484 KB
testcase_33 AC 22 ms
13,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int mod = 1000000007;
int n, m, d1, d2;

/*
[x^m] 1/(1-x) x/(1-x) (x^d1 + ... + x^d2)^{n-1}
[x^m] x/(1-x)^2 x^{d1 (n-1)} [(1 - x^{d2-d1+1}) / (1-x) ]^{n-1}
[x^{m-d1(n-1)-1}] (1 - x^{d2-d1+1})^{n-1} / (1-x)^{n+1}

[x^{k}] (1 - x^{e})^{n-1} / (1-x)^{n+1}

\sum_i (-1)^i\binom{n-1}{i} \binom{k-ie+n}{n}
=
\sum_i (-1)^i (k-ie+n)! / (n i! (n-1-i)! (k-ie)!) 
*/

int modpow(int x, int n){
  int r=1;
  for(;n;n>>=1){
    if(n&1) r = (long long) r * x % mod;
    x = (long long) x * x % mod;
  }
  return r;
}

int fact[1500000];
int ifact[1500000];

int main(){
  int i, k, e, r;
  scanf("%d%d%d%d",&n,&m,&d1,&d2);
  fact[0] = 1;
  for(i=1;i<sizeof(fact)/sizeof(fact[0]);i++)
    fact[i] = (long long) fact[i-1] * i % mod;
  i--;
  ifact[i]=modpow(fact[i], mod-2);
  for(i--;i>=0;i--)
    ifact[i] = (long long) ifact[i+1] * (i+1) % mod;
  if(m - 1 < (long long) d1*(n-1)){
    puts("0");
    return 0;
  }

  k = m - d1*(n-1) - 1;
  e = d2 - d1 + 1;

  r = 0;
  for(i=0; i<=n-1 && i*e <= k; i++){
    int z;
    z = (long long) fact[k-i*e+n] * ifact[i] % mod * ifact[n-1-i] % mod * ifact[k-i*e] % mod;
    if(i&1) r += mod-z;
    else r += z;
    if(r >= mod) r -= mod;
  }
  r = (long long) r * modpow(n, mod-2) % mod;
  printf("%d\n", r);

  return 0;
}
0