結果

問題 No.1202 お菓子の食べ方
ユーザー Shun_PIShun_PI
提出日時 2020-08-28 23:40:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 465 ms / 2,000 ms
コード長 1,940 bytes
コンパイル時間 1,589 ms
コンパイル使用メモリ 171,420 KB
実行使用メモリ 58,068 KB
最終ジャッジ日時 2024-04-26 14:03:03
合計ジャッジ時間 14,134 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 458 ms
57,776 KB
testcase_01 AC 465 ms
57,928 KB
testcase_02 AC 451 ms
57,920 KB
testcase_03 AC 462 ms
57,988 KB
testcase_04 AC 455 ms
58,032 KB
testcase_05 AC 460 ms
58,068 KB
testcase_06 AC 464 ms
57,984 KB
testcase_07 AC 449 ms
57,912 KB
testcase_08 AC 433 ms
57,984 KB
testcase_09 AC 442 ms
58,000 KB
testcase_10 AC 57 ms
50,716 KB
testcase_11 AC 96 ms
51,264 KB
testcase_12 AC 252 ms
54,220 KB
testcase_13 AC 59 ms
50,444 KB
testcase_14 AC 240 ms
54,036 KB
testcase_15 AC 49 ms
50,576 KB
testcase_16 AC 173 ms
52,780 KB
testcase_17 AC 104 ms
51,324 KB
testcase_18 AC 95 ms
51,156 KB
testcase_19 AC 53 ms
50,664 KB
testcase_20 AC 188 ms
53,104 KB
testcase_21 AC 206 ms
53,100 KB
testcase_22 AC 69 ms
50,592 KB
testcase_23 AC 161 ms
52,356 KB
testcase_24 AC 163 ms
52,380 KB
testcase_25 AC 60 ms
50,524 KB
testcase_26 AC 218 ms
53,412 KB
testcase_27 AC 183 ms
52,760 KB
testcase_28 AC 84 ms
50,976 KB
testcase_29 AC 108 ms
51,528 KB
testcase_30 AC 43 ms
50,436 KB
testcase_31 AC 42 ms
50,504 KB
testcase_32 AC 43 ms
50,528 KB
testcase_33 AC 44 ms
50,232 KB
testcase_34 AC 42 ms
50,320 KB
testcase_35 AC 42 ms
50,328 KB
testcase_36 AC 44 ms
50,388 KB
testcase_37 AC 43 ms
50,380 KB
testcase_38 AC 43 ms
50,412 KB
testcase_39 AC 44 ms
50,480 KB
testcase_40 AC 463 ms
57,928 KB
testcase_41 AC 434 ms
57,956 KB
testcase_42 AC 458 ms
57,988 KB
testcase_43 AC 438 ms
57,944 KB
testcase_44 AC 453 ms
57,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
constexpr int MAX = 2000000;
lint fact[MAX], finv[MAX], inv[MAX];
void cominit() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  FOR(i, 2, MAX){
    fact[i] = fact[i-1] * i % MOD;
    inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
    finv[i] = finv[i-1] * inv[i] % MOD;
  }
}
lint power(lint x, lint n, lint mod) {
  lint ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}
lint ncr(lint n, lint r, lint mod) {
  if(n < 0 || r < 0 || n-r < 0) return 0;
  return fact[n]*finv[r]%mod*finv[n-r]%mod;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lint N, M;
  cin >> N >> M;
  vector<vector<lint>> S(N+1, vector<lint>(M+1));
  REP(i, N) REP(j, M) cin >> S[i][j];
  lint ans = 0;
  cominit();
  REP(i, N) REP(j, M) {
    ans = (ans + ncr(S[i][j]-1+i+j, i+j, MOD) * ncr(i+j, j, MOD) % MOD) % MOD;
    ans = (ans + ((S[i][j] * ncr(S[i][j]+i+j, i+j, MOD) % MOD - S[i+1][j] * ncr(S[i+1][j]+i+j, i+j, MOD) % MOD + MOD ) * power(i+j+1, MOD-2, MOD) % MOD * ncr(i+j, j, MOD) % MOD)) % MOD;
    ans = (ans + ((S[i][j] * ncr(S[i][j]+i+j, i+j, MOD) % MOD - S[i][j+1] * ncr(S[i][j+1]+i+j, i+j, MOD) % MOD + MOD ) * power(i+j+1, MOD-2, MOD) % MOD * ncr(i+j, j, MOD) % MOD)) % MOD;
  }
  cout << ans << endl;
}
0