結果

問題 No.1520 Zigzag Sum
ユーザー tnakao0123tnakao0123
提出日時 2021-05-30 17:49:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 1,428 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 92,028 KB
実行使用メモリ 6,700 KB
最終ジャッジ日時 2023-08-08 15:11:39
合計ジャッジ時間 2,720 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
6,604 KB
testcase_01 AC 74 ms
6,624 KB
testcase_02 AC 93 ms
6,560 KB
testcase_03 AC 108 ms
6,604 KB
testcase_04 AC 108 ms
6,700 KB
testcase_05 AC 109 ms
6,620 KB
testcase_06 AC 104 ms
6,604 KB
testcase_07 AC 104 ms
6,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1520.cc:  No.1520 Zigzag Sum - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 200000;
const int MAX_W = 200000;
const int MAX_N = MAX_H + MAX_W;
const int MOD = 1000000007;

/* typedef */

typedef long long ll;

/* global variables */

int fs[MAX_N + 1], invfs[MAX_N + 1];

/* subroutines */

int powmod(int a, int n) {  // a^n % MOD
  int pm = 1;
  while (n > 0) {
    if (n & 1) pm = (ll)pm * a % MOD;
    a = (ll)a * a % MOD;
    n >>= 1;
  }
  return pm;
}

inline int nck(int n, int k) {  // nCk % MOD
  return (ll)fs[n] * invfs[n - k] % MOD * invfs[k] % MOD;
}

void prepare_fracs(int n) {
  fs[0] = invfs[0] = 1;
  for (int i = 1; i <= n; i++) {
    fs[i] = (ll)fs[i - 1] * i % MOD;
    invfs[i] = powmod(fs[i], MOD - 2);
  }
}

/* subroutines */

/* main */

int main() {
  prepare_fracs(MAX_N);

  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int h, w;
    scanf("%d%d", &h, &w);

    if (h == 1 || w == 1) puts("0");
    else {
      int a = (ll)nck(h + w - 4, h - 2) * (h + w - 3) % MOD * 2 % MOD;
      printf("%d\n", a);
    }
  }
  return 0;
}
0