这个比(1)复杂度更低,一般不会超时
#include <bits/stdc++.h> #define maxn 1000005 using namespace std; typedef long long ll; using namespace std; const ll mod=1e9+7; ll fact[maxn],inv[maxn]; ll Pow(ll x,ll n){ ll ans=1,base=x; while(n){ if(n&1) ans=ans*base%mod; base=base*base%mod; n>>=1; } return ans; } void init(){ fact[0]=1; for (int i = 1; i < maxn; ++i) { fact[i]=fact[i-1]*i%mod; } inv[maxn-1]=Pow(fact[maxn-1],mod-2); for (int i = maxn-2; i >= 0; --i) { inv[i]=inv[i+1]*(i+1)%mod; } } ll C(ll n, ll m) { if(n==m||m==0) return 1; return ((long long)fact[n]*inv[m]%mod)*inv[n-m]%mod; }
同样主函数要先init();
拿去不客气;