递推、杨辉三角
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7, N = 2010;
int n,a,b,c[N][N];
int main(){
for(int i=0;i<N;i++){
for(int j=0;j<=i;j++){
if(j==0) c[i][0] = 1;
else c[i][j] = (long long)(c[i-1][j-1]+c[i-1][j])%mod;
}
}
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
printf("%d\n",c[a][b]);
}
return 0;
}
乘法逆元、费马小定理
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7, N = 100010;
int n,a,b;
ll f[N] = {1},inf[N] = {1};
ll qp(ll a,ll b){
ll res = 1;
while(b){
if(b&1) res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
}
void init(){
for(int i=1;i<N;i++){
f[i] = (f[i-1]*i)%mod;
inf[i] = (inf[i-1])*qp(i,mod-2)%mod;
}
}
int C(int a,int b){
return f[a]*inf[b]%mod*inf[a-b]%mod;
}
int main(){
init();
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
printf("%d\n",C(a,b));
}
}
Lucas定理
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b;
int n,p;
ll qp(ll a,ll b,ll p){
ll res = 1;
while(b){
if(b&1) res = res*a%p;
a = a*a%p;
b >>= 1;
}
return res;
}
ll C(ll a,ll b){
ll res = 1;
for(ll i=1,j=a;i<=b;i++,j--){
res = res*j%p;
res = res*qp(i,p-2,p)%p;
}
return res;
}
ll Lucas(ll a,ll b,ll p){
if(a<p && b<p){
return C(a,b);
}
return C(a%p,b%p)*Lucas(a/p,b/p,p)%p;
}
int main(){
scanf("%d",&n);
while(n--){
scanf("%lld%lld%d",&a,&b,&p);
printf("%d\n",Lucas(a,b,p));
}
return 0;
}
欧拉筛、阶乘的质因子、高精度
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int primes[N] , cnt, sump[N];
bool vis[N];
void get_primes(int n){
for(int i=2;i<=n;i++){
if(!vis[i]) primes[cnt++] = i;
for(int j=0;primes[j]*i<=n;j++){
vis[primes[j]*i] = 1;
if(i%primes[j]==0) break;
}
}
}
int getNumOfP(int n,int p){
int res = 0;
while(n){
res += n/p;
n /= p;
}
return res;
}
vector<int> mul(const vector<int>& a,int b){
vector<int> c;
int t = 0;
for(int i=0;i<a.size();i++){
t += a[i]*b;
c.push_back(t%10);
t /= 10;
}
while(t){
c.push_back(t%10);
t /= 10;
}
return c;
}
int main(){
int a,b;
cin>>a>>b;
get_primes(a);
for(int i=0;i<cnt;i++){
int p = primes[i];
sump[i] = getNumOfP(a,p)-getNumOfP(b,p)-getNumOfP(a-b,p);
}
vector<int> ans = {1};
for(int i=0;i<cnt;i++)
for(int j=0;j<sump[i];j++)
ans = mul(ans,primes[i]);
for(int i=ans.size()-1;i>=0;i--) printf("%d",ans[i]);
return 0;
}