Pick http://blog.csdn.net/coldef/article/details/57908865
Last time to do a set of simulation game, which need to ask the natural number k power and, then I will only n^2 ... I remember N^2 has 20 points, Nlogn can burst to 90 points ...
--鏼 鏼 鏼 2015 national team paper
It's probably just this polynomial to get the inverse, find the generating function.
This is O (NLOGN) O (n\log N), but the modulus of this problem is not the prime number that satisfies the requirements of NTT ... The number of three NTT can be taken, making the product of these three primes greater than nP2 np^2, which can be merged with CRT.
As to why it is larger than nP2 np^2, because if the FFT directly, each number will not be greater than nP2 np^2, then if we choose three prime p1,p2,p3, the CRT can be used to find the remainder of the model P1*P2*P3, because p1*p2*p3>n*p^ 2, then to find out the model after the problem required modulus on it ... In fact, the principle is quite simple, but I understand that half a day ...
But directly on the CRT to explode longlong, you can use a small skill to deal with
--figure from Antileaf's blog
So every time the NTT is divided into three primes, and then merge, it's OK.
#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <
Cstring> using namespace std;
typedef long Long LL;
const int n=300010,p=1e9+7;
const int p1=998244353,p2=1004535809,p3=469762049;
Inline char NC () {static Char buf[100000],*p1=buf,*p2=buf; Return p1==p2&& (p2= (p1=buf) +fread (Buf,1,100000,stdin), P1==P2)?
eof:*p1++;
} inline void rea (int &x) {char c=nc (); x=0; For (;c> ' 9 ' | |
c< ' 0 '; c=nc ()); for (; c>= ' 0 ' &&c<= ' 9 '; x=x*10+c-' 0 ', C=NC ());
} inline void Rea (ll &x) {char c=nc (); x=0; For (;c> ' 9 ' | |
c< ' 0 '; c=nc ()); for (; c>= ' 0 ' &&c<= ' 9 '; x=x*10+c-' 0 ', C=NC ());
} inline ll Pow (ll x,ll y,ll p) {x%=p; ll ret=1;
for (; y;y>>=1,x=x*x%p) if (y&1) ret=ret*x%p;
return ret;
} const ll M_=1ll*p1*p2,inv1=pow (P1,P2-2,P2), Inv2=pow (P2,P1-2,P1), Inv3=pow (M_%P3,P3-2,P3);
int rev[n],fac[n],inv[n],a[n],b[n];
struct ntt{int p,num;
int w[2][n]; void Pre (int p,int n) {p=p; num=n;
int Ng=pow (3, (P-1)/num,p), Ig=pow (ng,p-2,p);
W[1][0]=w[0][0]=1;
for (int i=1;i<num;i++) w[1][i]=1ll*w[1][i-1]*ng%p,w[0][i]=1ll*w[0][i-1]*ig%p;
} void FFT (int *a,int n,int R) {for (int i=1;i<n;i++) if (rev[i]>i) swap (a[i],a[rev[i]);
for (int i=1;i<n;i<<=1) for (int j=0;j<n;j+= (i<<1)) for (int k=0;k<i;k++) {
int x=a[j+k],y=1ll*a[j+k+i]*w[r][num/(i<<1) *k]%p; a[j+k]= (x+y)%P;
a[j+k+i]= (x+p-y)%P;
} if (!r) for (int i=0,inv=pow (n,p-2,p); i<n;i++) a[i]=1ll*a[i]*inv%p;
}}ntt[3];
inline void Pre (int n) {fac[0]=1; for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%p; Inv[1]=1;
for (int i=2;i<=n;i++) inv[i]=1ll* (p-p/i) *inv[p%i]%p; Inv[0]=1;
for (int i=1;i<=n;i++) inv[i]=1ll*inv[i]*inv[i-1]%p;
} inline ll Mul (ll a,ll b,ll p) {a%=p; Return (a*b-(LL)(long double) a/p*b+1e-3) *p)%p+p)%p;
} inline int CRT (int a1,int a2,int A3) {ll a= (mul) 1ll*a1*p2%m_,inv2,m_ (+mul)) 1ll*a2*p1%m_,inv1,m_;
ll k= (1LL*A3+P3-A%P3) *inv3%p3;
Return (k* (m_%p) +a)%P;
int _b[3][n],__b[n];
void Inv (int *a,int *b,int n) {static int tmp[n];
if (n==1) return void (B[0]=CRT (Pow (A[0],P1-2,P1), Pow (A[0],P2-2,P2), Pow (A[0],P3-2,P3));
INV (a,b,n>>1); int l=0; while (!) (
n>>l&1)) l++; for (int i=1;i< (n<<1); i++) rev[i]= (rev[i>>1]>>1) |
((i&1) <<l);
for (int k=0;k<3;k++) for (int i=0;i<n;i++) _b[k][i]=b[i],_b[k][i+n]=0;
for (int k=0;k<3;k++) {for (int i=0;i<n;i++) tmp[i]=a[i],tmp[i+n]=0; NTT[K]. FFT (tmp,n<<1,1); NTT[K].
FFT (_b[k],n<<1,1); for (int i=0;i< (n<<1); i++) _b[k][i]=1ll*_b[k][i]*tmp[i]%ntt[k].
P NTT[K].
FFT (_b[k],n<<1,0); for (int i=0;i<n;i++) _b[0][i]=_b[1][i]=_b[2][i]= (P-CRT (_b[0][i],_b[1][i],_b[2)[i]))
%P;
_b[0][0]=_b[1][0]=_b[2][0]= (_b[0][0]+2)%P;
for (int k=0;k<3;k++) {for (int i=0;i<n;i++) tmp[i]=b[i],tmp[i+n]=0; NTT[K]. FFT (tmp,n<<1,1); NTT[K].
FFT (_b[k],n<<1,1); for (int i=0;i< (n<<1); i++) _b[k][i]=1ll*_b[k][i]*tmp[i]%ntt[k].
P NTT[K].
FFT (_b[k],n<<1,0);
for (int i=0;i<n;i++) B[I]=CRT (_b[0][i],_b[1][i],_b[2][i]);
int t,k;
ll N;
inline int C (int x,int y) {return 1ll*fac[x]*inv[y]%p*inv[x-y]%p;} int main () {int m=1; m<=50000;
M<<=1); Ntt[0].
Pre (p1,m<<1); NTT[1].
Pre (p2,m<<1); NTT[2].
Pre (p3,m<<1);
Pre (M);
for (int i=0;i<m;i++) a[i]=inv[i+1];
INV (A,B,M);
for (int i=0;i<m;i++) b[i]=1ll*b[i]*fac[i]%p;
B[1]=P-B[1];
REA (t);
while (t--) {rea (n); Rea (k); n%=p;
int ans=0;
for (int i=k,prod=n;~i;prod=1ll*prod*n%p,i--) ans= (1ll*prod*b[i]%p*c (k+1,i)%p+ans)%P; Ans=1ll*ans*pow (k+1,p-2, P)%P;
printf ("%d\n", ans);
return 0; }