对给定模数分解质因数后约分即可。依然常数巨大过不了。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 10010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} ll gcd(ll n,ll m){return m==0?n:gcd(m,n%m);} ll read() { ll x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,q,cnt,p[5000010],prime[500010],tot[100],t; ll g[100],a[25][N],b[N],c[N],d[N]; bool flag[5000010]; ll ksc(ll a,ll b,ll p) { ll t=a*b-(ll)((long double)a*b/p+0.5)*p; return t<0?t+p:t; } ll ksm(ll a,ll k,ll p) { ll s=1; for (;k;k>>=1,a=ksc(a,a,p)) if (k&1) s=ksc(s,a,p); return s; } void exgcd(ll &x,ll &y,ll a,ll b) { if (b==0) { x=1,y=0; return; } exgcd(x,y,b,a%b); ll t=x;x=y;y=t-a/b*x; } ll inv(ll a,ll p) { ll x,y;exgcd(x,y,a,p); x=(x%p+p)%p; return x; } bool check(int k,ll n) { if (ksm(k,n-1,n)!=1) return 0; ll p=n-1; while (!(p&1)) { p>>=1;ll x=ksm(k,p,n); if (x==n-1) return 1; if (x!=1) return 0; } return 1; } bool Miller_Rabin(ll n) { if (n<=5000000) return !flag[n]; return check(2,n)&&check(3,n)&&check(5,n)&&check(7,n)&&check(61,n)&&check(24251,n); } ll f(ll x,ll p,int c){return (ksc(x,x,p)+c)%p;} void getfactor(ll n) { if (n<=5000000) { while (n>1) g[++cnt]=p[n],n/=p[n]; return; } if (Miller_Rabin(n)) {g[++cnt]=n;return;} while (1) { int c=rand()%(n-1)+1; ll x=rand()%n,y=x; do { ll z=gcd(abs(x-y),n); if (z>1&&z<n) {getfactor(z),getfactor(n/z);return;} x=f(x,n,c),y=f(f(y,n,c),n,c); }while (x!=y); } } int main() { #ifndef ONLINE_JUDGE freopen("bzoj4891.in","r",stdin); freopen("bzoj4891.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif n=read(),m=read(),q=read(); flag[1]=1; for (int i=2;i<=5000000;i++) { if (!flag[i]) prime[++t]=i,p[i]=i; for (int j=1;j<=t&&prime[j]*i<=5000000;j++) { flag[prime[j]*i]=1; p[prime[j]*i]=prime[j]; if (i%prime[j]==0) break; } } for (int i=1;i<=m;i++) b[i]=read(); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) a[i][j]=read(); while (q--) { int x=read();ll y=read(),ans=1;cnt=0;getfactor(y); sort(g+1,g+cnt+1);cnt=unique(g+1,g+cnt+1)-g-1;memset(tot,0,sizeof(tot)); memcpy(c,b,sizeof(c));memcpy(d,a[x],sizeof(d)); for (int i=1;i<=m;i++) { for (int j=1;j<=cnt;j++) while (c[i]%g[j]==0) tot[j]++,c[i]/=g[j]; for (int j=1;j<=cnt;j++) while (d[i]%g[j]==0) tot[j]--,d[i]/=g[j]; ans=ksc(ksc(ans,c[i],y),inv(d[i],y),y); } for (int i=1;i<=cnt;i++) if (tot[i]<0) {ans=-1;break;} else ans=ksc(ans,ksm(g[i],tot[i],y),y); cout<<ans<<endl; } return 0; }