結果

問題 No.802 だいたい等差数列
ユーザー butsurizukibutsurizuki
提出日時 2019-11-20 16:52:03
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 2,278 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 18,284 KB
最終ジャッジ日時 2024-04-16 07:20:33
合計ジャッジ時間 4,151 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
18,060 KB
testcase_01 AC 16 ms
18,040 KB
testcase_02 AC 17 ms
18,244 KB
testcase_03 AC 16 ms
18,284 KB
testcase_04 AC 16 ms
18,120 KB
testcase_05 AC 19 ms
18,120 KB
testcase_06 AC 16 ms
18,044 KB
testcase_07 AC 16 ms
18,208 KB
testcase_08 AC 16 ms
18,068 KB
testcase_09 AC 17 ms
18,180 KB
testcase_10 AC 17 ms
18,200 KB
testcase_11 RE -
testcase_12 AC 18 ms
18,176 KB
testcase_13 RE -
testcase_14 AC 18 ms
18,272 KB
testcase_15 AC 18 ms
18,248 KB
testcase_16 AC 18 ms
18,208 KB
testcase_17 AC 18 ms
18,252 KB
testcase_18 AC 17 ms
18,056 KB
testcase_19 AC 18 ms
18,080 KB
testcase_20 AC 20 ms
18,216 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 19 ms
18,228 KB
testcase_24 AC 18 ms
18,088 KB
testcase_25 AC 17 ms
18,152 KB
testcase_26 AC 18 ms
18,208 KB
testcase_27 AC 18 ms
18,172 KB
testcase_28 AC 19 ms
18,256 KB
testcase_29 RE -
testcase_30 AC 16 ms
18,116 KB
testcase_31 AC 17 ms
18,064 KB
testcase_32 AC 16 ms
18,120 KB
testcase_33 AC 17 ms
18,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//nCr and [houjyo-genri]
//yukicoder No.802
#include<stdio.h>
#define mod 1000000007

long long power(long long a,long long b){
  long long x=1,y=a;
  while(b>0){
    if(b&1ll){
      x=(x*y)%mod;
    }
    y=(y*y)%mod;
    b>>=1;
  }
  return x%mod;
}

long long modular_inverse(long long n){
  return power(n,mod-2);
}

long long factorial[1048576];
long long invfact[1048576];

void cfact(){
  long long i;
  factorial[0]=1;
  factorial[1]=1;
  for(i=2;i<1048576;i++){
    factorial[i]=factorial[i-1]*i;
    factorial[i]%=mod;
  }
  invfact[1048575]=modular_inverse(factorial[1048575]);
  for(i=1048574;i>=0;i--){
    invfact[i]=invfact[i+1]*(i+1);
    invfact[i]%=mod;
  }
}

long long calcnCr(long long n,long long k){
  if(k<0 || n-k<0){return 0;}
  return (factorial[n]*((invfact[k]*invfact[n-k])%mod))%mod;
}

int main(void){
  cfact();
  long long i,j,n,m,k,a,b,c,h,w,r=0,l,t,d1,d;
  scanf("%lld%lld",&n,&m);
  scanf("%lld%lld",&d1,&d);
  m-=(d1*(n-1));
  d-=d1;
  //手を付けやすいように、A_iからd1*(i-1)を引いて条件を以下のように変換する。
  //1 <= A_1 <= A_2 <= ... <= A_n <= m
  //0 <= A_{i+1} - A_i <= d

  //条件(数え上げるべきものはこれら全てに違反するもので必要十分)
  //条件i : d < A_{i+1} - A_i
  //包除原理でこれら全てに違反するものを数え上げれば、それが解
  k=1;
  r=0;
  for(i=0;i<=n-1;i++){
    //i個の条件に違反する(「「それ以外はどちらでもよい」」)
    //まず、違反する条件をi個選ぶ。
    c=calcnCr(n-1,i);
    //このとき、ある条件iに違反するなら、A_{i+1}以降をすべて-(d+1)すればよい。
    //これで条件iには確実に違反できる。
    //繰り返しになるが、他の条件は「「満たしても満たさなくてもどちらでもよい」」
    //この条件たちを変換すると結局
    //0 <= B_1 <= B_2 <= ... <= B_n <= m-(d+1)*i-1
    //を満たす数列Bをすべて数え上げることになる。
    //これは
    // o が m-(d+1)*i-1 個、 | がn個の数え上げである。
    c*=calcnCr((m-(d+1)*i-1)+n,n);
    c%=mod;
    //あとは(-1)^nを掛けるあれ。
    r+=mod;
    r+=(c*k);
    r%=mod;
    k*=-1;
  }
  printf("%lld\n",r);
  return 0;
}
0