結果

問題 No.1141 田グリッド
ユーザー kappybarkappybar
提出日時 2020-07-31 22:51:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 3,410 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 174,352 KB
実行使用メモリ 16,860 KB
最終ジャッジ日時 2023-09-21 01:32:40
合計ジャッジ時間 6,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 136 ms
7,328 KB
testcase_14 AC 165 ms
16,860 KB
testcase_15 AC 138 ms
7,388 KB
testcase_16 AC 138 ms
7,300 KB
testcase_17 AC 139 ms
7,392 KB
testcase_18 AC 136 ms
7,356 KB
testcase_19 AC 135 ms
7,392 KB
testcase_20 AC 135 ms
7,320 KB
testcase_21 AC 137 ms
7,336 KB
testcase_22 AC 140 ms
7,280 KB
testcase_23 AC 140 ms
7,452 KB
testcase_24 AC 137 ms
7,412 KB
testcase_25 AC 138 ms
7,052 KB
testcase_26 AC 138 ms
7,420 KB
testcase_27 AC 137 ms
7,576 KB
testcase_28 AC 137 ms
7,096 KB
testcase_29 AC 134 ms
7,236 KB
testcase_30 AC 136 ms
7,496 KB
testcase_31 AC 135 ms
7,044 KB
testcase_32 AC 137 ms
7,268 KB
testcase_33 AC 138 ms
7,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

template<int mod> struct ModInt{
    long long x=0; 
    constexpr ModInt(long long x=0):x((x%mod+mod)%mod){}
    constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;}
    constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;}
    constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;}
    constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;}
    constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;}
    constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;}
    constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;}
    constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();}
    ModInt inv() const {
        long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1;
        while(s%t!=0){
            long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty;
            s=t;sx=tx;sy=ty;
            t=u;tx=ux;ty=uy;
        }
        return ModInt(tx);
    }
    ModInt pow(long long n) const {
        ModInt a=1;
        while(n>0){
            if(n&1) a*=*this;
            *this*=*this;
            n>>=1;
        }
        return a;
    }
    friend constexpr ostream& operator<<(ostream& os,const ModInt<mod>& a) {return os << a.x;}
    friend constexpr istream& operator>>(istream& is,ModInt<mod>& a) {return is >> a.x;}
};
using mint = ModInt<MOD>;

int main(){
    int h,w;
    cin >> h >> w;
    vector<vector<mint>> a(h,vector<mint>(w,0));
    rep(i,h)rep(j,w) cin >> a[i][j];
    
    vector<vector<mint>> ul(h,vector<mint>(w,0));
    vector<vector<mint>> ur(h,vector<mint>(w,0));
    vector<vector<mint>> dl(h,vector<mint>(w,0));
    vector<vector<mint>> dr(h,vector<mint>(w,0));
    rep(i,h)rep(j,w){
        ul[i][j]=ur[i][j]=dl[i][j]=dr[i][j]=a[i][j];
    }

    for(int i=0;i<h;i++){
        for(int j=1;j<w;j++){
            ul[i][j] *= ul[i][j-1];
        }
    }
    for(int j=0;j<w;j++){
        for(int i=1;i<h;i++){
            ul[i][j] *= ul[i-1][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=w-2;j>=0;j--){
            ur[i][j] *= ur[i][j+1];
        }
    }
    for(int j=0;j<w;j++){
        for(int i=1;i<h;i++){
            ur[i][j] *= ur[i-1][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=1;j<w;j++){
            dl[i][j] *= dl[i][j-1];
        }
    }
    for(int j=0;j<w;j++){
        for(int i=h-2;i>=0;i--){
            dl[i][j] *= dl[i+1][j];
        }
    }
    for(int i=0;i<h;i++){
        for(int j=w-2;j>=0;j--){
            dr[i][j] *= dr[i][j+1];
        }
    }
    for(int j=0;j<w;j++){
        for(int i=h-2;i>=0;i--){
            dr[i][j] *= dr[i+1][j];
        }
    }

    int q;
    cin >> q;
    while(q--){
        int r,c;
        cin >> r >> c;
        --r;--c;
        mint ans = 1;
        if(r-1>=0&&c-1>=0) ans *= ul[r-1][c-1];
        if(r-1>=0&&c+1<w) ans *= ur[r-1][c+1];
        if(r+1<h&&c-1>=0) ans *= dl[r+1][c-1];
        if(r+1<h&&c+1<w) ans *= dr[r+1][c+1];
        cout << ans << endl;
    }
    return 0;
}
0