(UVA - 10943)How do you add? (组合数学,隔板)

27 篇文章 0 订阅
9 篇文章 0 订阅

链接: https://vjudge.net/problem/UVA-10943

题意:把K个不超过N的非负整数加起来,使它们的和为N,有多少种方法?
例如:N=5,K=2有6种,0+5,1+4,2+3,3+2,4+1,5+2
输入N和K,(1<=N,K<=100),输出方法总数除以10^6的余数

分析:题意可以转化为 x1+x2+x3+……+xk=N的非负数解的种类数
由组合数可以得到答案是C(n+k-1,n)
相当于隔板

复杂度O(n^2)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <set>
#include <map>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
typedef long long LL;
const int mod=1e6;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int N=205;
int C[N][N];
void init()
{
    mem(C,0);
    for(int i=0; i<=N; i++)
        C[i][0]=C[i][i]=1;
    for(int i=1; i<=N; i++)
        for(int j=1; j<i; j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
int main()
{
    init();
    ios::sync_with_stdio(false);
    int n,k;
    while(cin>>n>>k)
    {
        if(!n&&!k) break;
        int ans=C[n+k-1][n];
        cout<<ans<<endl;
    }
    return 0;
}

可以进一步优化,复杂度为O(n)
其中用到了C(n,k+1)=C(n,k)*(n-k)/(k+1), 处理时注意溢出的情况。
写了一下,但是WA了,应该不会溢出吧?

WA的代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <set>
#include <map>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
typedef long long LL;
const int mod=1e6;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int N=205;
LL C[N][N];
LL n,k;
LL pow_mod(LL a,LL b)
{
    LL ans=1;
    a%=mod;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
LL fermat(LL a,LL p)
{
    return pow_mod(a,p-2);
}
void init(int n)
{
    C[n+k-1][0]=1;
    for(LL i=1; i<n; i++)
    {
        C[n+k-1][i]=(1LL*C[n+k-1][i-1]*(n+k-1-i+1)*pow_mod(i,mod))%mod;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>k)
    {
        if(!n&&!k) break;
        init(n);
        int ans=C[n+k-1][k-1];
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值