結果

問題 No.1141 田グリッド
ユーザー mtsdmtsd
提出日時 2020-07-31 21:58:31
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 6,754 bytes
コンパイル時間 1,195 ms
コンパイル使用メモリ 122,976 KB
実行使用メモリ 19,084 KB
最終ジャッジ日時 2023-09-20 23:12:22
合計ジャッジ時間 5,930 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,960 KB
testcase_01 AC 3 ms
5,880 KB
testcase_02 AC 4 ms
5,684 KB
testcase_03 AC 3 ms
5,756 KB
testcase_04 AC 3 ms
5,820 KB
testcase_05 AC 4 ms
5,668 KB
testcase_06 AC 3 ms
5,684 KB
testcase_07 AC 3 ms
5,728 KB
testcase_08 AC 6 ms
6,028 KB
testcase_09 AC 5 ms
5,856 KB
testcase_10 AC 6 ms
5,940 KB
testcase_11 AC 6 ms
5,908 KB
testcase_12 AC 5 ms
6,016 KB
testcase_13 AC 137 ms
9,136 KB
testcase_14 AC 166 ms
19,084 KB
testcase_15 AC 136 ms
7,928 KB
testcase_16 AC 135 ms
7,800 KB
testcase_17 AC 136 ms
7,904 KB
testcase_18 AC 135 ms
7,760 KB
testcase_19 AC 133 ms
7,776 KB
testcase_20 AC 133 ms
7,780 KB
testcase_21 AC 136 ms
7,764 KB
testcase_22 AC 136 ms
7,784 KB
testcase_23 AC 134 ms
7,708 KB
testcase_24 AC 134 ms
7,788 KB
testcase_25 AC 134 ms
7,516 KB
testcase_26 AC 134 ms
7,864 KB
testcase_27 AC 134 ms
7,840 KB
testcase_28 AC 135 ms
7,484 KB
testcase_29 AC 134 ms
7,752 KB
testcase_30 AC 133 ms
7,868 KB
testcase_31 AC 132 ms
7,788 KB
testcase_32 AC 136 ms
7,692 KB
testcase_33 AC 135 ms
7,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
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;
}

template <unsigned int mod>
class ModInt {
private:
    unsigned int v;
    static unsigned int norm(const unsigned int& x){ return x < mod ? x : x - mod; }
    static ModInt make(const unsigned int& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static unsigned int inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    unsigned int operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        unsigned int v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

#define MOD 1000000007
using mod = ModInt<MOD>;

#define MAX_N 200000
mod inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i=2;i<MAX_N;i++){
        inv[i] = MOD - inv[MOD % i] * (MOD / i);
        fac[i] = fac[i-1] * i;
        finv[i] = finv[i-1] * inv[i];
    }
}

mod comb(int a, int b)
{
    if(a<b) return 0;
    return fac[a] * finv[b] * finv[a-b];
}

int main(){
    int n,m,q;
    cin >> n >> m;
    vector<vector<mod> > a(n+2,vector<mod>(m+2,1)),b(n+2,vector<mod>(m+2,1)),c(n+2,vector<mod>(m+2,1)),d(n+2,vector<mod>(m+2,1));
    vector<vector<mod> > v(n,vector<mod>(m));
    rep(i,n){
        rep(j,m){
            cin >> v[i][j];
        }
    }
    rep(i,n){
        rep(j,m){
            a[i+1][j+1] *= v[i][j];
            a[i+1][j+1] *= a[i+1][j];
        }
        rep(j,m){
            a[i+1][j+1] *= a[i][j+1];
        }
    }
    rep(i,n){
        for(int j=m-1;j>=0;j--){
            b[i+1][j+1] *= v[i][j];
            b[i+1][j+1] *= b[i+1][j+2];
        }
        rep(j,m){
            b[i+1][j+1] *= b[i][j+1];
        }
    }
    for(int i=n-1;i>=0;i--){
        rep(j,m){
            c[i+1][j+1] *= v[i][j];
            c[i+1][j+1] *= c[i+1][j];
        }
        rep(j,m){
            c[i+1][j+1] *= c[i+2][j+1];
        }
    }
    for(int i=n-1;i>=0;i--){
        for(int j=m-1;j>=0;j--){
            d[i+1][j+1] *= v[i][j];
            d[i+1][j+1] *= d[i+1][j+2];
        }
        rep(j,m){
            d[i+1][j+1] *= d[i+2][j+1];
        }
    }
    cin >> q;
    rep(zz,q){
        int rr,cc;
        cin >> rr >> cc;
        mod res = 1;
        res *= a[rr-1][cc-1];
        // cerr << a[rr-1][cc-1] << " ";
        res *= b[rr-1][cc+1];
        // cerr << b[rr-1][cc+1] << " ";
        res *= c[rr+1][cc-1];
        // cerr << c[rr+1][cc-1] << " ";
        res *= d[rr+1][cc+1];
        // cerr << d[rr+1][cc+1] << endl;
        cout << res << "\n";
    }
    return 0;
}
0