結果

問題 No.802 だいたい等差数列
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2019-09-21 00:33:44
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,252 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 30,360 KB
実行使用メモリ 12,852 KB
最終ジャッジ日時 2023-10-13 09:18:34
合計ジャッジ時間 2,249 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 15 ms
12,096 KB
testcase_11 AC 18 ms
12,692 KB
testcase_12 AC 17 ms
12,172 KB
testcase_13 AC 18 ms
12,560 KB
testcase_14 AC 19 ms
12,240 KB
testcase_15 AC 17 ms
12,016 KB
testcase_16 AC 19 ms
12,512 KB
testcase_17 AC 18 ms
12,364 KB
testcase_18 AC 18 ms
12,504 KB
testcase_19 AC 19 ms
12,828 KB
testcase_20 AC 21 ms
12,840 KB
testcase_21 AC 21 ms
12,852 KB
testcase_22 AC 19 ms
12,776 KB
testcase_23 AC 19 ms
12,840 KB
testcase_24 AC 19 ms
12,588 KB
testcase_25 AC 15 ms
12,068 KB
testcase_26 AC 6 ms
6,988 KB
testcase_27 AC 19 ms
12,648 KB
testcase_28 AC 11 ms
10,092 KB
testcase_29 AC 21 ms
12,660 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 10 ms
10,024 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<n+m;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