結果
| 問題 |
No.1141 田グリッド
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-01 01:38:09 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,305 bytes |
| コンパイル時間 | 1,286 ms |
| コンパイル使用メモリ | 123,648 KB |
| 実行使用メモリ | 8,704 KB |
| 最終ジャッジ日時 | 2024-07-07 00:18:34 |
| 合計ジャッジ時間 | 12,041 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 WA * 10 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
using int64 = long long;
const int MOD = 1000000007;
struct mint {
int64 x;
mint(const int64 x1=0) { x = x1%MOD; };
mint& operator++(int n) { x+=1; return *this; };
mint& operator--(int n) { x-=1; return *this; };
mint& operator=(int64 n) { x=n%MOD; return *this; };
mint& operator--() {x-=1; return *this; };
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; return *this; }
mint& operator/=(const mint a) { return (*this) *= a.inv();}
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 operator/(const mint a) const { mint res(*this); return res/=a; }
mint inv() const { return pow(MOD-2);}
friend ostream& operator<<(ostream &os, const mint a) noexcept { return os << a.x; }
constexpr bool operator == (const mint& r) const noexcept { return this->x == r.x; }
constexpr bool operator != (const mint& r) const noexcept { return this->x != r.x; }
mint pow(long long t) const {
if (!t) return mint(1);
mint a = pow(t>>1);
a*=a;
if(t&1) a*= *this;
return a;
}
};
long long pow(long long a, long long n, long long p) {
long long res = 1;
while(n!=0) {
if (n%2) {
res *= a;
res %= p;
}
a *= a;
a %= p;
n >>= 1;
}
return res % p;
}
long long inv(long long a) {
return pow(a, MOD-2, MOD);
}
int main() {
int h,w;
cin >> h >> w;
vector<vector<long long>> a(h, vector<long long>(w,0));
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
cin >> a[i][j];
}
}
vector<vector<long long>> cpro(h+1, vector<long long>(w+1, 1));
for (int i=1; i<=h; i++) {
for (int j=1; j<=w; j++) {
cpro[i][j] = (cpro[i-1][j] * cpro[i][j-1])%MOD;
(cpro[i][j] *= pow(cpro[i-1][j-1],MOD-2,MOD)) %= MOD;
(cpro[i][j] *= a[i-1][j-1]) %= MOD;
}
}
for (int i=0; i<=h; i++) {
for (int j=0; j<=w; j++) {
cerr << cpro[i][j] << " ";
}
cerr<<endl;
}
int q;
cin >> q;
for (int i=0; i<q; i++) {
int r,c;
cin >> r >> c;
mint ans = cpro[r-1][c-1]; // 左上
// 右上
ans *= cpro[r-1][w];
ans *= inv(cpro[r-1][c]);
// 左下
ans *= cpro[h][c-1];
ans *= inv(cpro[r][c-1]);
// 右下
ans *= cpro[h][w];
ans *= inv(cpro[h][c]);
ans *= inv(cpro[r][w]);
ans *= cpro[r][c];
cout << ans << endl;
}
}
/*
* 1 2 3
* 4 5 6
* 7 8 9
*
* 000001 000001 000001 000001
* 000001 000001 000002 000006
* 000001 000004 000040 000720
* 000001 000028 002240 362880
* 5*6*8*9 = 2160
* (3612880 / 6) / 28 = 2160
*
*
* 累積積
* 01 02 03 04
* 05 06 07 08
* 09 10 11 12
* 13 14 15 16
*
* x^p ≡ x
* x^{p-1} ≡ 1 mod p
* x^{p-2} ≡ 1/x mod p
*/