結果

問題 No.1141 田グリッド
ユーザー Plan8Plan8
提出日時 2020-07-31 22:14:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 4,072 bytes
コンパイル時間 3,459 ms
コンパイル使用メモリ 208,668 KB
実行使用メモリ 8,824 KB
最終ジャッジ日時 2023-09-20 23:47:42
合計ジャッジ時間 6,471 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 7 ms
4,380 KB
testcase_11 AC 10 ms
4,380 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 125 ms
8,824 KB
testcase_14 AC 128 ms
8,780 KB
testcase_15 AC 124 ms
7,412 KB
testcase_16 AC 124 ms
7,328 KB
testcase_17 AC 123 ms
7,420 KB
testcase_18 AC 40 ms
7,292 KB
testcase_19 AC 38 ms
7,248 KB
testcase_20 AC 39 ms
7,468 KB
testcase_21 AC 125 ms
7,248 KB
testcase_22 AC 124 ms
7,252 KB
testcase_23 AC 124 ms
7,340 KB
testcase_24 AC 100 ms
7,264 KB
testcase_25 AC 100 ms
7,428 KB
testcase_26 AC 99 ms
7,260 KB
testcase_27 AC 101 ms
7,360 KB
testcase_28 AC 100 ms
7,324 KB
testcase_29 AC 78 ms
7,296 KB
testcase_30 AC 77 ms
7,340 KB
testcase_31 AC 55 ms
7,304 KB
testcase_32 AC 94 ms
7,408 KB
testcase_33 AC 94 ms
7,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct mint {
    long long x;
    mint(long long x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
    	return *this;
    }
    mint& operator*=(const mint a) {
    	(x *= a.x) %= MOD;
        if(x < 0) x += MOD;
    	return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
    	if (t&1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
    	return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

void Main(){
    int H,W; cin >> H >> W;
    
    vector<int> hz(H,0), wz(W,0);
    int nz = 0;

    ll A[H][W];
    REP(i,H)REP(j,W){
        cin >> A[i][j];
        if(A[i][j] == 0){
            hz[i]++; wz[j]++;
            nz++;
        }
    }

    mint s[4][H+1][W+1];
    REP(j,4)REP(i,H+1) s[j][i][0] = 1;
    REP(j,4)REP(i,W+1) s[j][0][i] = 1;

    REP(i,H)REP(j,W){
        if(s[0][i+1][j].x==0 || s[0][i][j+1].x==0) s[0][i+1][j+1] = 0;
        else s[0][i+1][j+1] = s[0][i+1][j]*s[0][i][j+1]/s[0][i][j]*A[i][j];
    }
    REP(i,H)REP(j,W){
        if(s[1][i+1][j].x==0 || s[1][i][j+1].x==0) s[1][i+1][j+1] = 0;
        else s[1][i+1][j+1] = s[1][i+1][j]*s[1][i][j+1]/s[1][i][j]*A[H-1-i][j];
    }
    REP(i,H)REP(j,W){
        if(s[2][i+1][j].x==0 || s[2][i][j+1].x==0) s[2][i+1][j+1] = 0;
        else s[2][i+1][j+1] = s[2][i+1][j]*s[2][i][j+1]/s[2][i][j]*A[H-1-i][W-1-j];
    }
    REP(i,H)REP(j,W){
        if(s[3][i+1][j].x==0 || s[3][i][j+1].x==0) s[3][i+1][j+1] = 0;
        else s[3][i+1][j+1] = s[3][i+1][j]*s[3][i][j+1]/s[3][i][j]*A[i][W-1-j];
    }

    int Q; cin >> Q;
    REP(_,Q){
        int r,c; cin >> r >> c;

        VI h{r-1,H-r,H-r,r-1}, w{c-1,c-1,W-c,W-c};
        mint ans(1); REP(i,4) ans *= s[i][h[i]][w[i]];
        cout << ans.x << en;
    }
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}
0