結果

問題 No.1141 田グリッド
ユーザー fastmathfastmath
提出日時 2020-07-31 21:42:46
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 3,857 bytes
コンパイル時間 2,296 ms
コンパイル使用メモリ 133,608 KB
実行使用メモリ 10,060 KB
最終ジャッジ日時 2023-09-20 22:25:42
合計ジャッジ時間 4,843 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,440 KB
testcase_01 AC 2 ms
7,544 KB
testcase_02 AC 3 ms
7,456 KB
testcase_03 AC 2 ms
7,544 KB
testcase_04 AC 3 ms
9,496 KB
testcase_05 AC 3 ms
7,428 KB
testcase_06 AC 3 ms
7,424 KB
testcase_07 AC 3 ms
7,440 KB
testcase_08 AC 4 ms
9,512 KB
testcase_09 AC 3 ms
7,524 KB
testcase_10 AC 4 ms
7,496 KB
testcase_11 AC 4 ms
7,476 KB
testcase_12 AC 4 ms
7,700 KB
testcase_13 AC 39 ms
9,932 KB
testcase_14 AC 42 ms
9,016 KB
testcase_15 AC 38 ms
8,000 KB
testcase_16 AC 38 ms
8,108 KB
testcase_17 AC 37 ms
8,212 KB
testcase_18 AC 28 ms
8,260 KB
testcase_19 AC 29 ms
8,384 KB
testcase_20 AC 28 ms
8,128 KB
testcase_21 AC 38 ms
10,060 KB
testcase_22 AC 38 ms
8,016 KB
testcase_23 AC 37 ms
8,172 KB
testcase_24 AC 29 ms
8,016 KB
testcase_25 AC 29 ms
8,356 KB
testcase_26 AC 29 ms
8,600 KB
testcase_27 AC 28 ms
8,368 KB
testcase_28 AC 28 ms
8,400 KB
testcase_29 AC 29 ms
8,180 KB
testcase_30 AC 28 ms
8,248 KB
testcase_31 AC 29 ms
8,324 KB
testcase_32 AC 29 ms
8,228 KB
testcase_33 AC 29 ms
8,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int N = 1e5+7, MOD = 1000 * 1000 * 1000 + 7;

//need define int long long
int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }

struct M {
ll x;
M (int x_) {
    x = mod(x_);
}   
M () {}
M operator + (M y) {
    int ans = x + y.x;
    if (ans >= MOD)
        ans -= MOD;
    return M(ans);
}
M operator - (M y) {
    int ans = x - y.x;
    if (ans < 0)
        ans += MOD;
    return M(ans);            
}   
M operator * (M y) {
    return M(x * y.x % MOD);   
}   
M operator / (M y) {
    return M(x * fp(y.x, MOD - 2) % MOD);
}   
M operator + (int y) {
    return (*this) + M(y);
}
M operator - (int y) {
    return (*this) - M(y);
}   
M operator * (int y) {
    return (*this) * M(y);
}   
M operator / (int y) {
    return (*this) / M(y);
}   
M operator ^ (int p) {
    return M(fp(x, p));
}   
void operator += (M y) {
    *this = *this + y;
}   
void operator -= (M y) {
    *this = *this - y;
}   
void operator *= (M y) {
    *this = *this * y;
}
void operator /= (M y) {
    *this = *this / y;
}   
void operator += (int y) {
    *this = *this + y;
}   
void operator -= (int y) {
    *this = *this - y;
}   
void operator *= (int y) {
    *this = *this * y;
}
void operator /= (int y) {
    *this = *this / y;
}   
void operator ^= (int p) {
    *this = *this ^ p;
}
};  

M f[N], inv[N];
void prec() {
    f[0] = M(1);
    for (int i = 1; i < N; ++i)
        f[i] = f[i - 1] * M(i);
    inv[N - 1] = f[N - 1] ^ (MOD - 2);
    for (int i = N - 2; i >= 0; --i)
        inv[i] = inv[i + 1] * M(i + 1);
}
M C(int n, int k) {
    if (n < k)
        return M(0);
    else
        return f[n] * inv[k] * inv[n - k];
}   


vector <M> a[N];
M row[N], col[N];

int row0[N], col0[N], all0 = 0;

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        a[i].resize(m);
        for (int j = 0; j < m; ++j) {
            int x;
            cin >> x;
            a[i][j] = M(x);
        }   
    }   
        
    for (int i = 0; i < n; ++i) {
        row[i] = M(1);
        for (int j = 0; j < m; ++j) {
            if (a[i][j].x == 0)
                ++row0[i];
            else
                row[i] *= a[i][j];
        }   
    }   

    for (int j = 0; j < m; ++j) {
        col[j] = M(1);
        for (int i = 0; i < n; ++i) {
            if (a[i][j].x == 0)
                ++col0[j];
            else
                col[j] *= a[i][j];
        }   
    }   

    M all = M(1);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j) {
            if (a[i][j].x == 0)
                ++all0;
            else
                all *= a[i][j];
        }

    int q;
    cin >> q;
    while (q--) {
        int i, j;
        cin >> i >> j;
        --i; --j;

        int nul = all0 - row0[i] - col0[j] + (a[i][j].x == 0);
        if (nul > 0) {
            cout << 0 << endl;
        }   
        else {
            if (a[i][j].x)
                cout << (all / (row[i] * col[j]) * a[i][j]).x << endl;
            else
                cout << (all / (row[i] * col[j])).x << endl;                
        }
    }   
}
0