結果

問題 No.1202 お菓子の食べ方
ユーザー KudeKude
提出日時 2020-08-29 05:29:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 453 ms / 2,000 ms
コード長 3,010 bytes
コンパイル時間 2,067 ms
コンパイル使用メモリ 173,844 KB
実行使用メモリ 50,432 KB
最終ジャッジ日時 2024-04-26 15:58:53
合計ジャッジ時間 17,562 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 435 ms
50,092 KB
testcase_01 AC 442 ms
50,176 KB
testcase_02 AC 426 ms
50,312 KB
testcase_03 AC 435 ms
50,360 KB
testcase_04 AC 437 ms
50,176 KB
testcase_05 AC 443 ms
50,176 KB
testcase_06 AC 434 ms
50,176 KB
testcase_07 AC 436 ms
50,176 KB
testcase_08 AC 436 ms
50,176 KB
testcase_09 AC 453 ms
50,304 KB
testcase_10 AC 84 ms
42,496 KB
testcase_11 AC 126 ms
43,520 KB
testcase_12 AC 268 ms
46,592 KB
testcase_13 AC 90 ms
42,624 KB
testcase_14 AC 258 ms
46,356 KB
testcase_15 AC 81 ms
42,440 KB
testcase_16 AC 197 ms
45,008 KB
testcase_17 AC 132 ms
43,520 KB
testcase_18 AC 124 ms
43,392 KB
testcase_19 AC 87 ms
42,624 KB
testcase_20 AC 210 ms
45,192 KB
testcase_21 AC 217 ms
45,376 KB
testcase_22 AC 97 ms
42,804 KB
testcase_23 AC 174 ms
44,708 KB
testcase_24 AC 180 ms
44,672 KB
testcase_25 AC 88 ms
42,624 KB
testcase_26 AC 230 ms
45,568 KB
testcase_27 AC 195 ms
44,928 KB
testcase_28 AC 110 ms
43,136 KB
testcase_29 AC 135 ms
43,648 KB
testcase_30 AC 73 ms
42,224 KB
testcase_31 AC 72 ms
42,240 KB
testcase_32 AC 73 ms
42,240 KB
testcase_33 AC 73 ms
42,368 KB
testcase_34 AC 73 ms
42,368 KB
testcase_35 AC 74 ms
42,368 KB
testcase_36 AC 72 ms
42,308 KB
testcase_37 AC 72 ms
42,240 KB
testcase_38 AC 73 ms
42,240 KB
testcase_39 AC 72 ms
42,240 KB
testcase_40 AC 441 ms
50,176 KB
testcase_41 AC 442 ms
50,260 KB
testcase_42 AC 440 ms
50,304 KB
testcase_43 AC 444 ms
50,432 KB
testcase_44 AC 444 ms
50,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

#define MOD 1000000007
//#define MOD 998244353
struct mint {
	int i;
	mint() : i(0) {}
	mint(int x) {
		i = int(x % MOD);
		if (i < 0) i += MOD;
	}
	template<class T> mint(T x) {
		i = int(x % MOD);
		if (i < 0) i += MOD;
	}
	mint operator+(const mint x) const {return i + x.i;}
	mint operator-(const mint x) const {return i - x.i;}
	mint operator*(const mint x) const {return (long long)i * x.i;}
	mint operator/(const mint x) const {return (long long)i * x.pow(MOD - 2).i;}
	mint inv() {return pow(MOD - 2);}
	template<class T> mint pow(T p) const {
		long long r = 1;
		long long t = i;
		for(; p > 0; p >>= 1) {
			if (p & 1) r = r * t % MOD;
			t = t * t % MOD;
		}
		return r;
	}
	template<class T1, class T2> static mint pow(T1 a, T2 b) {
		long long r = 1;
		long long t = (long long)(a % MOD);
		for(; b > 0; b >>= 1) {
			if (b & 1) r = r * t % MOD;
			t = t * t % MOD;
		}
		return r;
	}
	mint& operator+=(const mint x) {
		i = (i + x.i) % MOD;
		return *this;
	}
	mint& operator-=(const mint x) {
		i = i - x.i;
		if (i < 0) i += MOD;
		return *this;
	}
	mint& operator*=(const mint x) {
		i = (int)((long long)i * x.i % MOD);
		return *this;
	}
	mint& operator/=(const mint x) {
		i = (long long)i * x.pow(MOD - 2).i % MOD;
		return *this;
	}
};

std::ostream& operator<<(std::ostream& os, const mint& m) {
	return os << m.i;
}

struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < MOD);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
} comb(5e6);

int main() {
    int n, m;
    cin >> n >> m;
    VVI s(n, VI(m));
    rep(i, n) rep(j, m) cin >> s[i][j];
    mint ans = 0;

    VVI blockmax = s;
    rrep(i, n) rrep(j, m) {
        if (i + 1 < n) chmax(blockmax[i][j], blockmax[i+1][j]);
        if (j + 1 < m) chmax(blockmax[i][j], blockmax[i][j+1]);
    }
    rep(i, n) rep(j, m) {
        if (i == n - 1) {
            ans += comb(i + j, i) * comb(i + j + s[i][j], i + j + 1);
        } else {
            ans += comb(i + j, i) * (comb(i + j + s[i][j], i + j + 1) - comb(i + j + blockmax[i + 1][j], i + j + 1));
        }
        if (j == m - 1) {
            ans += comb(i + j, i) * comb(i + j + s[i][j], i + j + 1);
        } else {
            ans += comb(i + j, i) * (comb(i + j + s[i][j], i + j + 1) - comb(i + j + blockmax[i][j + 1], i + j + 1));
        }
        ans += comb(i + j, i) * comb(i + j + s[i][j] - 1, i + j);
    }
    cout << ans << endl;
}
0