結果
問題 | No.1141 田グリッド |
ユーザー |
|
提出日時 | 2020-07-31 22:35:46 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 42 ms / 2,000 ms |
コード長 | 1,545 bytes |
コンパイル時間 | 284 ms |
コンパイル使用メモリ | 34,176 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-06 19:49:28 |
合計ジャッジ時間 | 2,383 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 31 |
ソースコード
#include<cstdio>const long long mod = 1e9+7;long long a[100005];long long row[100005], col[100005];int cntR[100005], cntC[100005];long long getElement(int i,int j,int n,int m){int id = (i-1)*m+j;return a[id];}long long power(long long a,long long b){long long res = 1;while(b){if(b&1) res = res*a%mod;a = a*a%mod;b /= 2;}return res;}long long inv(long long u){ return power(u,mod-2); }int main(){int n,m;scanf("%d%d",&n,&m);for(int i = 1; i <= n; i++) row[i] = 1, cntR[i] = 0;for(int i = 1; i <= m; i++) col[i] = 1, cntC[i] = 0;long long all = 1; int cnt = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){int id = (i-1)*m+j;scanf("%lld",&a[id]);if(a[id]==0){cntR[i]++;cntC[j]++;cnt++;}else{row[i] = row[i]*a[id]%mod;col[j] = col[j]*a[id]%mod;all = all*a[id]%mod;}}}int q;scanf("%d",&q);while(q--){int r,c;scanf("%d%d",&r,&c);int leftCnt = cnt-cntR[r]-cntC[c];if(getElement(r,c,n,m)==0) leftCnt++;if(leftCnt==0){long long res = all*inv(row[r])%mod*inv(col[c])%mod;long long middle = getElement(r,c,n,m);if(middle!=0) res = res*middle%mod;printf("%lld\n",res);}else printf("0\n");}return 0;}