結果

問題 No.1141 田グリッド
ユーザー umezoumezo
提出日時 2020-08-02 03:42:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,564 bytes
コンパイル時間 1,947 ms
コンパイル使用メモリ 203,476 KB
実行使用メモリ 7,264 KB
最終ジャッジ日時 2023-09-23 03:45:13
合計ジャッジ時間 9,587 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 6 ms
4,384 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 152 ms
6,612 KB
testcase_14 AC 154 ms
7,264 KB
testcase_15 AC 132 ms
5,792 KB
testcase_16 AC 136 ms
5,712 KB
testcase_17 AC 132 ms
5,736 KB
testcase_18 AC 129 ms
5,684 KB
testcase_19 AC 129 ms
5,692 KB
testcase_20 AC 130 ms
5,956 KB
testcase_21 AC 133 ms
5,692 KB
testcase_22 AC 134 ms
5,776 KB
testcase_23 AC 132 ms
5,696 KB
testcase_24 AC 129 ms
5,876 KB
testcase_25 AC 130 ms
5,768 KB
testcase_26 AC 132 ms
5,736 KB
testcase_27 AC 130 ms
5,832 KB
testcase_28 AC 132 ms
5,900 KB
testcase_29 AC 130 ms
5,740 KB
testcase_30 AC 129 ms
5,776 KB
testcase_31 AC 131 ms
5,692 KB
testcase_32 AC 128 ms
5,712 KB
testcase_33 AC 131 ms
5,724 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:57:6: 警告: ‘tmp’ may be used uninitialized [-Wmaybe-uninitialized]
   57 |   ll tmp;
      |      ^~~

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int MOD=1e9+7;

ll modinv(ll a,ll m){
  ll b=m,u=1,v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a,b);
    u-=t*v; swap(u,v);
  }
  u%=m; 
  if(u<0) u+=m;
  return u;
}

int main(){
  ll h,w;
  cin>>h>>w;
  
  ll A[h+1][w+1],Z[h+1][w+1];
  ll x;
  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      cin>>x;
      if(x==0){
        A[i][j]=1;
        Z[i][j]=1;
      }
      else{
        A[i][j]=x;
        Z[i][j]=0;
      }
    }
  }
  
  ll s[h+2][w+2];
  rep(i,h+2){
    rep(j,w+2) s[i][j]=0;
  }
  s[1][1]=0;
  for(int i=1;i<h+1;i++){
    for(int j=1;j<w+1;j++) s[i+1][j+1]=s[i+1][j]+s[i][j+1]-s[i][j]+Z[i][j];
  }
  
  ll all=1;
  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++) all=(all*A[i][j])%MOD;
  }
  
  vector<ll> B(h+1),C(w+1);
  ll tmp;
  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      if(j==1) tmp=A[i][1];
      else tmp=(tmp*A[i][j])%MOD;
    }
    B[i]=modinv(tmp,MOD);
  }
  for(int j=1;j<=w;j++){
    for(int i=1;i<=h;i++){
      if(i==1) tmp=A[1][j];
      else tmp=(tmp*A[i][j])%MOD;
    }
    C[j]=modinv(tmp,MOD);
  }
  
  ll q;
  cin>>q;
  
  ll r,c;
  rep(i,q){
    cin>>r>>c;
    ll a=0;
    a+=s[r][c];
    a+=s[r][w+1]-s[r][c+1];
    a+=s[h+1][c]-s[r+1][c];
    a+=s[h+1][w+1]-s[r+1][w+1]-s[h+1][c+1]+s[r+1][c+1];
    if(a>0) cout<<0<<endl;
    else{
      ll ans=(((all*B[r]%MOD)*C[c]%MOD)*A[r][c])%MOD;
      cout<<ans<<endl;
    }
  }

  return 0;
}
0