結果
問題 | No.1202 お菓子の食べ方 |
ユーザー |
![]() |
提出日時 | 2020-08-29 05:29:32 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 437 ms / 2,000 ms |
コード長 | 3,010 bytes |
コンパイル時間 | 2,083 ms |
コンパイル使用メモリ | 177,932 KB |
実行使用メモリ | 50,336 KB |
最終ジャッジ日時 | 2024-11-14 05:03:34 |
合計ジャッジ時間 | 16,588 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 45 |
ソースコード
#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 998244353struct 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;}