G26 求组合数 快速幂

视频链接:G26 求组合数 快速幂_哔哩哔哩_bilibili

#include <iostream>
using namespace std;

typedef long long LL;
const int N=100010,P=1e9+7;
int q,n,m;
LL f[N],g[N];

LL qsm(LL a,int b){
  LL res=1;
  for(;b;a=a*a%P,b>>=1) if(b&1)res=res*a%P;
  return res;
}
LL C(LL n,LL m){
  return f[n]*g[m]%P*g[n-m]%P;
}
int main(){
  f[0]=g[0]=1;
  for(int i=1;i<N;i++){
    f[i]=f[i-1]*i%P;
    g[i]=qsm(f[i],P-2)%P;
  }
  cin>>q;
  while(q--){
    cin>>n>>m;
    printf("%lld\n",C(n,m));
  }
  return 0;
}

 

#include <iostream>
using namespace std;

typedef long long LL;
const int N=100010, P=1e9+7;
LL f[N], g[N];

LL qpow(LL a, int b){
  LL res = 1;
  while(b){
    if(b & 1) res=res*a%P;
    a = a*a%P;
    b >>= 1;
  }
  return res;
}
void init(){
  f[0] = g[0] = 1;
  for(int i=1; i<N; i++){
    f[i] = f[i-1]*i%P;
    g[i] = g[i-1]*qpow(i,P-2)%P;
  }  
}
LL getC(LL n, LL m){
  return f[n]*g[m]%P*g[n-m]%P;
}
int main(){
  init();
  int q, n, m;
  cin >> q;
  while(q--){
    cin >> n >> m;
    printf("%lld\n", getC(n,m));
  }
  return 0;
}

 

posted @ 2022-10-08 20:16  董晓  阅读(834)  评论(0)    收藏  举报