結果

問題 No.1388 Less than K
ユーザー 蜜蜂蜜蜂
提出日時 2020-11-23 01:34:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,200 ms / 3,000 ms
コード長 2,179 bytes
コンパイル時間 1,602 ms
コンパイル使用メモリ 171,760 KB
実行使用メモリ 491,392 KB
最終ジャッジ日時 2023-08-17 11:40:08
合計ジャッジ時間 19,163 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
15,236 KB
testcase_01 AC 14 ms
15,196 KB
testcase_02 AC 15 ms
15,292 KB
testcase_03 AC 15 ms
15,196 KB
testcase_04 AC 14 ms
15,240 KB
testcase_05 AC 24 ms
19,752 KB
testcase_06 AC 19 ms
17,636 KB
testcase_07 AC 14 ms
15,208 KB
testcase_08 AC 35 ms
24,864 KB
testcase_09 AC 16 ms
15,580 KB
testcase_10 AC 16 ms
15,648 KB
testcase_11 AC 15 ms
15,272 KB
testcase_12 AC 32 ms
23,292 KB
testcase_13 AC 47 ms
29,996 KB
testcase_14 AC 66 ms
38,652 KB
testcase_15 AC 159 ms
80,440 KB
testcase_16 AC 233 ms
113,688 KB
testcase_17 AC 362 ms
171,464 KB
testcase_18 AC 254 ms
15,208 KB
testcase_19 AC 365 ms
15,248 KB
testcase_20 AC 395 ms
15,168 KB
testcase_21 AC 53 ms
31,732 KB
testcase_22 AC 19 ms
15,192 KB
testcase_23 AC 23 ms
18,312 KB
testcase_24 AC 33 ms
22,664 KB
testcase_25 AC 39 ms
25,316 KB
testcase_26 AC 29 ms
21,148 KB
testcase_27 AC 22 ms
17,824 KB
testcase_28 AC 16 ms
15,400 KB
testcase_29 AC 16 ms
15,288 KB
testcase_30 AC 16 ms
15,264 KB
testcase_31 AC 16 ms
15,516 KB
testcase_32 AC 15 ms
15,168 KB
testcase_33 AC 17 ms
15,140 KB
testcase_34 AC 18 ms
15,292 KB
testcase_35 AC 17 ms
15,204 KB
testcase_36 AC 18 ms
15,208 KB
testcase_37 AC 18 ms
15,188 KB
testcase_38 AC 85 ms
45,372 KB
testcase_39 AC 37 ms
26,828 KB
testcase_40 AC 64 ms
35,984 KB
testcase_41 AC 192 ms
88,984 KB
testcase_42 AC 33 ms
24,176 KB
testcase_43 AC 303 ms
15,140 KB
testcase_44 AC 701 ms
15,244 KB
testcase_45 AC 385 ms
15,316 KB
testcase_46 AC 505 ms
15,432 KB
testcase_47 AC 498 ms
15,256 KB
testcase_48 AC 642 ms
15,208 KB
testcase_49 AC 518 ms
15,188 KB
testcase_50 AC 506 ms
15,200 KB
testcase_51 AC 409 ms
15,168 KB
testcase_52 AC 340 ms
15,204 KB
testcase_53 AC 206 ms
96,448 KB
testcase_54 AC 137 ms
67,084 KB
testcase_55 AC 34 ms
25,836 KB
testcase_56 AC 383 ms
178,768 KB
testcase_57 AC 1,200 ms
491,392 KB
testcase_58 AC 566 ms
15,188 KB
testcase_59 AC 429 ms
15,232 KB
testcase_60 AC 348 ms
15,208 KB
testcase_61 AC 21 ms
15,176 KB
testcase_62 AC 18 ms
15,192 KB
testcase_63 AC 37 ms
25,940 KB
testcase_64 AC 46 ms
32,020 KB
testcase_65 AC 147 ms
69,448 KB
testcase_66 AC 213 ms
97,700 KB
testcase_67 AC 233 ms
107,020 KB
testcase_68 AC 729 ms
335,100 KB
testcase_69 AC 900 ms
413,252 KB
testcase_70 AC 820 ms
15,184 KB
testcase_71 AC 503 ms
15,188 KB
testcase_72 AC 25 ms
15,192 KB
testcase_73 AC 20 ms
15,184 KB
testcase_74 AC 19 ms
15,176 KB
testcase_75 AC 18 ms
15,192 KB
testcase_76 AC 18 ms
15,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define fi first
#define se second

constexpr ll mod=998244353;

const int MAX = 505000;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(int i=2;i<MAX;i++){
        fac[i]=fac[i-1]*i%mod;
        inv[i]=mod-inv[mod%i]*(mod/i)%mod;
        finv[i]=finv[i-1]*inv[i]%mod;
    }
}

// 二項係数計算
long long COM(int n,int k){
    if(n<k) return 0;
    if(n<0||k<0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%mod)%mod;
}

void solve1(int h,int w,int k){
  int x=min(h,w);
  vector<vector<int>> dp(x+1,vector<int>(2*k+1,0));
  //dp[i][j] := move2 i times & distance j-k
  dp[0][k]=1;
  for(int i=0;i<=x;i++){
    for(int j=2*k;j>=0;j--){
      if(j-2>=0){
        dp[i][j-2]+=dp[i][j];
        if(dp[i][j-2]>mod){
          dp[i][j-2]-=mod;
        }
      }
    }    
    if(i!=x){
      for(int j=0;j<=k;j++){
        int r=k-j,s=k+j;
        if(r+2<=2*k){
          dp[i+1][r+2]+=dp[i][r];
          if(dp[i+1][r+2]>mod){
            dp[i+1][r+2]-=mod;
          }
        }
        if(r!=s){
          r=s;
          if(r+2<=2*k){
            dp[i+1][r+2]+=dp[i][r];
            if(dp[i+1][r+2]>mod){
              dp[i+1][r+2]-=mod;
            }
          }
        }
      }
    }    
  }
      
  ll ans=0;
  for(int i=0;i<=x;i++){
    ll way=dp[i][k];
    way*=(COM(h+w,h-i)*COM(w+i,w-i))%mod;
    way%=mod;
    ans+=way;
    ans%=mod;
  }
  cout<<ans<<endl;
}

void solve2(int h,int w,int k){
  int x=min(h,w);
  ll ans=0;
  for(int i=0;i<=x;i++){
    int kk=(k+2)/2;
    int a=1;
    ll way=COM(2*i,i);
    while(i-a*kk>=0){
      if(a%2==1){
        way-=2*COM(2*i,i-a*kk);
        way%=mod;
      }
      else{
        way+=2*COM(2*i,i-a*kk);
        way%=mod;
      }
      a++;
    }
    if(way<0){
      way+=mod*100;
      way%=mod;
    }
    way*=(COM(h+w,h-i)*COM(w+i,w-i))%mod;
    way%=mod;
    ans+=way;
    ans%=mod;
  }
  cout<<ans<<endl;
}

int main(){
  COMinit();
  int h,w,k;
  cin>>h>>w>>k;
  h--;
  w--;
  if(k<=300){
    solve1(h,w,k);
  }
  else{
    solve2(h,w,k);
  }
}
0