1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ putchar(x?'1':'0'); } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1e6+10; const ll Mod=1e9+7; const int S=1e6; int Test,N,M; ll H[MAXN],Fib[MAXN]; ll Mu[MAXN],Pri[MAXN],Tot; ll Inv_fib[MAXN],Mul_H[MAXN]; bool Is[MAXN]; inline ll qPow(ll a,ll b) { ll res=1; while(b) { if(b&1) res=res*a%Mod; a=a*a%Mod;b>>=1; } return res; } inline void sieve(int n) { Mu[1]=1,Is[1]=1; for(int i=2;i<=n;++i) { if(!Is[i]) Pri[++Tot]=i,Mu[i]=-1; for(int j=1;j<=Tot&&i*Pri[j]<=n;++j) { Is[i*Pri[j]]=1; if(i%Pri[j]==0) { Mu[i*Pri[j]]=0; break; } Mu[i*Pri[j]]=-Mu[i]; } } } inline void init() { sieve(S); Fib[0]=0,Fib[1]=Inv_fib[1]=1; for(int i=2;i<=S;++i) Fib[i]=(Fib[i-1]+Fib[i-2])%Mod; for(int i=2;i<=S;++i) Inv_fib[i]=qPow(Fib[i],Mod-2); for(int i=1;i<=S;++i) H[i]=1ll; for(int i=1;i<=S;++i) { if(!Mu[i]) continue; for(int j=1;i*j<=S;++j) H[i*j]=H[i*j]*(Mu[i]==1?Fib[j]:Inv_fib[j])%Mod; } Mul_H[0]=1; for(int i=1;i<=S;++i) Mul_H[i]=Mul_H[i-1]*H[i]%Mod; } inline int getEd(int x,int n){ return n/(n/x); } inline ll solve(int n,int m) { ll res=1; int ed=std::min(n,m); for(int l=1,r=0;l<=ed;l=r+1) { r=std::min(std::min(getEd(l,n),getEd(l,m)),ed); ll btm=Mul_H[r]*qPow(Mul_H[l-1],Mod-2)%Mod; res=res*qPow(btm,1ll*(n/l)*(m/l)%(Mod-1))%Mod; } return res; } int main() { read(Test); init(); while(Test--) { read(N,M); write(solve(N,M),'\n'); } return 0; }
|