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 @   董晓  阅读(583)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示