组合公式

组合公式C(n,m);
http://codevs.cn/problem/1631/

http://baike.baidu.com/link?url=WfX9ZlRyN8huoWRxkjBRCMg0VhG2whWCu-ZwlqIsiY3fvpHbbrCV9Rtjkcqt41_LsERSmxXoIxs5RtQOebdfkIpfmz2Jv-8MAfzrrgFYKkj71D6jazrLAnG11cwdMkiu

暴力

#include<bits/stdc++.h>
#define Ll long long
using namespace std;
int n,m;
Ll mo=100003;
Ll ksm(Ll x,int y){
    Ll ans=1;
    for(;y;y/=2,x=x*x%mo)
        if(y&1)ans=ans*x%mo;
    return ans;
}
Ll C(int n,int m){
    Ll ans=1;
    for(int i=1;i<=m;i++)
        ans=ans*(n-i+1)%mo*ksm(i,mo-2)%mo;
    return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
    printf("%lld",C(n,m));
}

杨辉三角(666)离线
杨辉三角的这个j< i是可以变成i< min(i,m+1)的

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define Ll long long
using namespace std;
Ll mo=100003;
Ll C[1001][1001];
int n,m,x,y,z;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++)C[i][0]=C[i][i]=1;
    for(int i=2;i<=n;i++)
    for(int j=1;j<i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mo;
    printf("%lld",C[n][m]);
}

递归调用法(其实就是杨辉三角的在线版)

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define Ll long long
using namespace std;
Ll mo=100003;
Ll b[1001][1001];
Ll n,m,x,y,z;
Ll C(Ll n,Ll m){
    if(b[n][m])return b[n][m];
    if(m==0||m==n)return 1;
    if(m>n)return 0;
    b[n][m]=(C(n-1,m)+C(n-1,m-1))%mo;
    return b[n][m];
}
int main()
{
    scanf("%lld%lld",&n,&m);
    printf("%lld",C(n,m));
}

最后放上卢卡斯定力的

#include<bits/stdc++.h>
#define Ll long long
using namespace std;
Ll n,m,mo;
Ll ksm(Ll x,Ll y){
    Ll ans=1;
    for(;y;y/=2,x=x*x%mo)
        if(y&1)ans=ans*x%mo;
    return ans;
}
Ll C(Ll n,Ll m){
    if(m>n)return 0;
    Ll ans=1;
    for(Ll i=1;i<=m;i++)
        ans=ans*(n-i+1)%mo*ksm(i,mo-2)%mo;
    return ans;
}
Ll lucas(Ll n,Ll m){
    if(m==0)return 1;
    return lucas(n/mo,m/mo)*C(n%mo,m%mo)%mo;
}
int main()
{
    scanf("%lld%lld%lld",&n,&m,&mo);
    printf("%lld\n",lucas(n+m,n));
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值