結果
| 問題 |
No.1141 田グリッド
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2020-08-01 09:51:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 79 ms / 2,000 ms |
| コード長 | 5,579 bytes |
| コンパイル時間 | 3,254 ms |
| コンパイル使用メモリ | 206,376 KB |
| 最終ジャッジ日時 | 2025-01-12 12:22:55 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
struct Ruisekiwa2D {
int VH, VW;
using T = int;
vector<vector<T>> v;
bool needBuild = false;
Ruisekiwa2D() { }
Ruisekiwa2D(int w, int h) { init(w, h); }
void init(int w, int h) {
needBuild = true;
VH = h; VW = w;
v.clear();
v.resize(h, vector<T>(w));
rep(y, 0, VH) rep(x, 0, VW) v[y][x] = 0; }
void set(int x, int y, T c) { v[y][x] = c; }
void add(int x, int y, T c) { v[y][x] += c; }
void build() {
rep(y, 0, VH) rep(x, 0, VW) {
if (0 < y) v[y][x] += v[y - 1][x];
if (0 < x) v[y][x] += v[y][x - 1];
if (0 < y && 0 < x) v[y][x] -= v[y - 1][x - 1];
}
needBuild = false;
}
// [sx,sy]~[tx,ty]
T get(int sx, int tx, int sy, int ty) {
if (needBuild) assert(0 && "Need Build");
if (tx < sx or ty < sy) return 0;
T rs = v[ty][tx];
if (0 < sx) rs -= v[ty][sx - 1];
if (0 < sy) rs -= v[sy - 1][tx];
if (0 < sx && 0 < sy) rs += v[sy - 1][sx - 1];
return rs;
}
T getToDown(int x, int y, int len) {
if (needBuild) assert(0 && "Need Build");
if (VH - y < len) len = VH - y;
return get(x, x, y, y + len - 1);
}
T getToUp(int x, int y, int len) {
if (needBuild) assert(0 && "Need Build");
if (y + 1 < len) len = y + 1;
return get(x, x, y - len + 1, y);
}
};
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int H, W, Q;
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> H >> W;
vector<vector<int>> A(H, vector<int>(W));
rep(y, 0, H) rep(x, 0, W) cin >> A[y][x];
Ruisekiwa2D r2d(W, H);
rep(y, 0, H) rep(x, 0, W) if (A[y][x] == 0) {
r2d.add(x, y, 1);
A[y][x] = 1;
}
r2d.build();
vector<mint> columns(W, 1);
rep(x, 0, W) rep(y, 0, H) columns[x] *= A[y][x];
vector<mint> rows(H, 1);
rep(x, 0, W) rep(y, 0, H) rows[y] *= A[y][x];
mint tot = 1;
rep(y, 0, H) rep(x, 0, W) tot *= A[y][x];
cin >> Q;
rep(_, 0, Q) {
int y, x; cin >> y >> x;
y--; x--;
int zero = 0;
zero += r2d.get(0, x - 1, 0, y - 1); // LU
zero += r2d.get(0, x - 1, y + 1, H - 1); // LD
zero += r2d.get(x + 1, W - 1, 0, y - 1); // RU
zero += r2d.get(x + 1, W - 1, y + 1, H - 1); // RD
if (0 < zero) printf("0\n");
else {
mint ans = tot / columns[x] / rows[y] * A[y][x];
printf("%d\n", ans.get());
}
}
}
はまやんはまやん