結果

問題 No.1202 お菓子の食べ方
ユーザー leaf_1415leaf_1415
提出日時 2020-08-30 01:21:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 2,006 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 81,120 KB
実行使用メモリ 42,540 KB
最終ジャッジ日時 2024-04-27 01:24:47
合計ジャッジ時間 9,046 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 183 ms
42,520 KB
testcase_01 AC 179 ms
42,328 KB
testcase_02 AC 180 ms
42,496 KB
testcase_03 AC 180 ms
42,324 KB
testcase_04 AC 180 ms
42,320 KB
testcase_05 AC 180 ms
42,488 KB
testcase_06 AC 178 ms
42,468 KB
testcase_07 AC 192 ms
42,352 KB
testcase_08 AC 179 ms
42,504 KB
testcase_09 AC 181 ms
42,472 KB
testcase_10 AC 36 ms
35,816 KB
testcase_11 AC 51 ms
36,244 KB
testcase_12 AC 110 ms
42,448 KB
testcase_13 AC 37 ms
35,524 KB
testcase_14 AC 127 ms
42,464 KB
testcase_15 AC 34 ms
35,868 KB
testcase_16 AC 82 ms
40,404 KB
testcase_17 AC 55 ms
38,144 KB
testcase_18 AC 52 ms
35,932 KB
testcase_19 AC 36 ms
35,244 KB
testcase_20 AC 89 ms
42,492 KB
testcase_21 AC 89 ms
40,428 KB
testcase_22 AC 38 ms
37,172 KB
testcase_23 AC 75 ms
42,288 KB
testcase_24 AC 76 ms
38,416 KB
testcase_25 AC 38 ms
37,780 KB
testcase_26 AC 94 ms
40,556 KB
testcase_27 AC 82 ms
42,392 KB
testcase_28 AC 45 ms
38,324 KB
testcase_29 AC 59 ms
38,356 KB
testcase_30 AC 32 ms
34,832 KB
testcase_31 AC 32 ms
34,984 KB
testcase_32 AC 31 ms
34,884 KB
testcase_33 AC 31 ms
34,716 KB
testcase_34 AC 32 ms
35,416 KB
testcase_35 AC 31 ms
36,320 KB
testcase_36 AC 32 ms
35,224 KB
testcase_37 AC 34 ms
35,572 KB
testcase_38 AC 33 ms
35,616 KB
testcase_39 AC 31 ms
34,692 KB
testcase_40 AC 181 ms
42,492 KB
testcase_41 AC 180 ms
42,540 KB
testcase_42 AC 181 ms
42,408 KB
testcase_43 AC 182 ms
42,464 KB
testcase_44 AC 183 ms
42,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

const int FACT_MAX = 2000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

llint h, w;
llint a[1005][1005];

llint get(llint x, llint y, llint z)
{
	return comb(x+y, y) * comb(x+y+z, z) % mod;
}
llint sum(llint x, llint y, llint h)
{
	if(h < 0) return 0;
	return comb(x+y+h+1, h) * comb(x+y, y) % mod;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	make_fact();
	
	cin >> h >> w;
	for(int y = 0; y < h; y++){
		for(int x = 0; x < w; x++){
			cin >> a[x][y];
		}
	}
	
	llint ans = 0;
	for(int y = 0; y < h; y++){
		for(int x = 0; x < w; x++){
			ans += sum(x, y, a[x][y]-1) + mod - sum(x, y, a[x+1][y]-1), ans %= mod;
			ans += sum(x, y, a[x][y]-1) + mod - sum(x, y, a[x][y+1]-1), ans %= mod;
			ans += get(x, y, a[x][y]-1), ans %= mod;
		}
	}
	cout << ans << endl;
	
	return 0;
}
0