[SDOI2017]数字表格

SOL:

  怎么说呢,套路题。随便数论分块就好了

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define N 1000007
#define LL long long
#define pri p
#define mo 1000000007
using namespace std;
int pri[N/10],tot,u[N],usd[N];
LL f[N],ff[N],ax,g[N];
inline LL qsm(LL x,LL y=mo-2) {
    static LL anw;
    for (anw=1,x=x%mo;y;y>>=1,x=x*x%mo) if (y&1) anw=anw*x%mo;
    return anw;
} 
void pre(){
    for (int i=2;i<N;i++) {
       if (!usd[i]) pri[++tot]=i,u[i]=-1;
       for (int j=1;j<=tot&&i*pri[j]<N;j++) {
            if (i%p[j]==0)  { u[i*p[j]]=0; usd[i*p[j]]=1; break;}
            u[i*p[j]]=-u[i]; usd[i*p[j]]=1;
       }
    }
    ff[0]=ff[1]=f[1]=1; u[1]=1; g[1]=1;
    for (int i=2;i<N;i++) f[i]=f[i-1]+f[i-2],f[i]%=mo,g[i]=qsm(f[i]),ff[i]=1;
    for (int i=1;i<N;i++) {
        if (!u[i]) continue;
        for (int j=i;j<N;j+=i) 
         ff[j]=ff[j]*(u[i]==1?f[j/i]:g[j/i])%mo;
    }
    for (int i=2;i<N;i++) ff[i]=ff[i-1]*ff[i]%mo;
}
int n,m,T;
LL ni,ans;
signed main () {
//    freopen("a.in","r",stdin);
    pre();
    scanf("%d",&T);
    while (T--) {
        scanf("%d%d",&n,&m);
        if (n>m) swap(n,m); ans=1;
        for (int i=1,last;i<=n;i=last+1) {
            last=min(n/(n/i),m/(m/i));
            ni=qsm(ff[i-1]);
            ans=ans*qsm(ff[last]*ni,1ll*(n/i)*(m/i))%mo;
        }
        printf("%lld\n",ans);
    } return 0;
}

 

posted @ 2018-04-19 20:11  泪寒之雪  阅读(164)  评论(0编辑  收藏  举报