2014年1月7日 星期二

[ZJ] a545. Stressful


#include<stdio.h>
#include<algorithm>
#define mod 100000007LL
using namespace std;
int s[1005][1005];
long long int all,ans;
long long int mal(long long int a,int c){
    if(c==1) return a;
    long long int tmp=mal(a,c/2);
    tmp=tmp*tmp%mod;
    if(c%2==0) return tmp;
    else return tmp*a%mod;
}
int ReadInt(int *x) {
    static char c, neg;
    while((c = getchar()) < '-')    {if(c == EOF) return EOF;}
    neg = (c == '-') ? -1 : 1;
    *x = (neg == 1) ? c-'0' : 0;
    while((c = getchar()) >= '0')
        *x = (*x << 3) + (*x << 1) + c-'0';
    *x *= neg;
    return 1;
}
int main(){
    int n,m,x,y,t,i,j;
    while(scanf("%d%d",&n,&m)!=EOF && n+m){
        all=1;
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                ReadInt(&s[i][j]);
                all*=s[i][j];
                if(all>s[i][j]) all%=mod;
            }
        }
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&x,&y);
            printf("%lld\n",all*mal(s[x-1][y-1],mod-2)%mod);
        }
    }
}

沒有留言:

張貼留言