隔板法 例题集

(1)

 uva 10943 ------how do you add? (N<=1000,可采用组合数打表的方法)

#include <iostream>
#include <cstdio>
using namespace std;
int C[300][300];

void init()
{
    C[0][0]=1;
    for(int i=1;i<300;i++)
        for(int j=0;j<=i;j++)
    {
        if(i==j)
            C[i][j]=1;
        else if(j==0)
            C[i][j]=1;
        else
        {
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%1000000;
        }
    }
}
int main()
{
    init();
    int n,k;
    while (scanf("%d%d",&n,&k)&&n!=0&&k!=0)
    {
        cout<<C[n+k-1][k-1]<<endl;
    }

    return 0;
}

(3)ZOJ(3557)------How Many Sets II  (mod数大于N,M 的情况,采用卢卡斯定理)

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll t,n,m,p,ans;

ll qpow(ll n,ll k,ll q)
{
    ll res=1;
    while(k)
    {
        if(k&1) res=res*n%p;
        n=n*n%p; k>>=1;
    }return res;
}
ll c(ll n,ll m,ll p)
{
    ll n1=1,m1=1;
    if(m>n) return 0;
    for(int i=1;i<=m;i++)
     m1=m1*i%p;
    for(int i=n-m+1;i<=n;i++)
     n1=n1*i%p;
    return n1*qpow(m1,p-2,p)%p;
}
ll lus(ll n,ll m)
{
    if(m==0) return 1;
    return c(n%p,m%p,p)*lus(n/p,m/p)%p;
}
int main()
{
    while(scanf("%lld%lld%lld",&n,&m,&p)!=EOF)
    {
        ans=lus(n-m+1,m);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41199502/article/details/84258080
今日推荐