问题如下:
今天在写一道组合数的题目,然后
ans=ans*Pow(jc(n-m),Mod-2)%Mod;
//为什么我写成ans*=Pow(jc(n-m),Mod-2)%Mod就错了
C++小白,求教。
完整代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll Mod=1e9+7;
ll Pow(ll a,ll b){
ll ans=1;
while(b){
if(b%2==1){
ans=ans*a%Mod;
}
a=a*a%Mod;
b=b/2;
}
return ans;
}
ll jc(ll x){
ll t=1;
for(ll i=1;i<=x;i++){
t=t*i%Mod;
}
t=t%Mod;
return t;
}
ll C(ll n,ll m){
ll ans=1;
ans*=jc(n)%Mod;
ans*=Pow(jc(m),Mod-2)%Mod;
ans=ans*Pow(jc(n-m),Mod-2)%Mod; //为什么我写成ans*=Pow(jc(n-m),Mod-2)%Mod就错了
return ans;
}
int main(){
ios::sync_with_stdio(false);
ll a,b;
cin>>a>>b;
//cout<<jc(4)<<endl;
//cout<<Pow(2,3)<<endl;
//组合数
cout<<C(a+b-2,b-1)<<endl;
return 0;
}