How do you add?(UVA 10943)

C - How do you add? 很裸的隔板法。 引用一下维基上对隔板法的解释: 现在有10个球,要放进3个盒子里 ●●●●●●●●●● 隔2个板子,把10个球被隔开成3个部份 ●|●|●●●●●●●●、●|●●|●●●●●●●、●|●●●|●●●●●●、●|●●●●|●●●●●、●|●●●●●|●●●●、●|●●●●●●|●●●、… 如此类推,10个球放进3个盒子的方法总数为\binom {10-1}{3-1}=\binom {... 阅读详情

题目链接
这题正解其实是用隔板法,ans(n,k)=C(n+k-1,k-1),因为这题n比较小,所以可以直接用C(n,m)=C(n-1,m-1)+C(n-1,M)打出杨辉三角,时空复杂度都是O(n²)。
然后我用的方法是DP,ans(n,k)=∑(i:0->n)ans(i,k-1), 时间复杂度O(n³)。
附上AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<cmath>
#define pq priority_queue
#define Pi acos(-1.0)
#define MAXX 1000000007
using namespace std;
int gcd(int a,int b)
{
    if(a%b)
        return gcd(b,a%b);
    else
        return b;
}
int dp[105][105];
int main()
{
    int n,k;
    for(int i=0;i<=100;i++)
        dp[i][1]=1;
    for(int j=2;j<=100;j++)
        for(int i=0;i<=100;i++)
        {
            for(int m=0;m<=i;m++)
                dp[i][j]+=dp[i-m][j-1];
            dp[i][j]%=1000000;
        }
    while(scanf("%d%d",&n,&k),n+k!=0)
    {
        printf("%d\n",dp[n][k]);
    }
    return 0;
}

Memory: 0 KB Time: 0 MS
Language: C++ 4.8.2 Result: Accepted

UVa10943 - How do you add?(组合数学) 简介:把K个不超过N的非负整数加起来,使ta们的和为N分析:相当于有N个小球,要放到K个盒子里,允许有空盒子,问方案数 ans=C(N+K-1,K-1)tip读入的时候如果不写!=EOF,读入就不会正常结束,对拍的时候就拍不起来//这里写代码片 #include<cstdio> #include<cstring> #include<iostream>using namespace std;cons 阅读详情

相关推荐

How do you add?

How do you add? Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a

沧6月的专栏 610

C - How do you add

Larry is very bad at math — he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a snowboarding accide...

qq_42865713的博客 339

How do you add?(递推)

题意:求将n分为k个数相加的种数。 如:n=20,k=2,则可分为: 0+20=20 1+19=20 2+18=20 ....... 20 +0=20 共21种方案。 解析:令f(n,m)表示将n分为m个数相加的种数,则 f(n,m)= ∑ f(n-i,m-1) (0 ≤ i ≤n) 代码如下: 1 # include<iostream>...

weixin_30795127的博客 166

uva 10943 - How do you add?(dp)

题目连接:uva 10943 - How do you add? 题目大意:给出n和k,用k个数组成加法算式,使得最后的和为n,求有多少种组合方式。 解题思路:一开始不知道0也算,即比如说 1  100, 可以使0 + 0 + 1,所以说答案是100。num[i][j] = ∑ num[k][j - 1](0 ≤ k ≤ i)。 #include #includ

不慌不忙、不急不躁 1542

UVA10943 - How do you add?(DP

UVA10943 - How do you add?(DP) 题目链接 题目大意:给你N和K,要求找出这样的K个数(小于等于N),和等于N,问有多少种结果模上1e6。 解题思路:dp。 代码: #include #include const int maxn = 105; const int MOD = 1e6; typedef long long ll; ll

579

uva10943 How do you add?(DP

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=1884 #include <iostream> #include <cstring> #...

akk99296的博客 131

uva 10943How do you add?(简单组合)

Problem A: How do you add? Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ry

1328

UVA 10943 - How do you add?(dp)

Problem A: How do you add? Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ry

Remilia's 1205

UVA 10943 How do you add?(隔板法)

题目:求多少个组合使得k个不大于n的数(可以取0)的和是n。 主要应用组合数学中的隔板法:将n个物体放进k个篮子里,篮子可以是空的,一共有多少种方法。如果篮子必须是非空的,那么就有C(n-1,k-1)种方法。但这里可以是空的,于是假设再加入k个物体,提前在每个篮子都放上一个,再进行分离。所以C(n-1,k-1)变成C(n+k-1,k-1). #include #include using n

theArcticOcean 879

UVA10943- How do you add?(隔板法)

题目链接 题意:求由K个不大于N的非负整数组合而成N的方法数。 思路:隔板法。这个问题可以等价于有N个小球放入K个盒子中,盒子可以为空,问有几种放置的方法。但因为盒子为空,不符合隔板法,所以我们可以先往每个盒子里面放置一个小球,满足每个盒子都不为空的,等分完后,在每个盒子减去一个小球。所以就相当与将N + K - 1个小球分为K - 1份。之后就是组合数学的问题了。

懒人一枚。 1179

UVA 题目10943 How do you add?(DP,整数拆分)

Larry is very bad at math — he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a snowboarding acc

Jogging_Clown 824

UVa 10943 - How do you add?(组合数

求一个数n拆成k个数的方法个数。 利用隔板法求得ans={{n+k-1}\choose{k-1}} 递推组合数即可。

wcr1996的专栏 1915

UVa 10943 How do you add?(数学递推)

相当于求解方程x1+x2+......+xk=N。把这个方程转化为(x1+1)+(x2+1)+......+(xk+1)=N+K,这样每一项都是大于等于1的。那就相当于把N+K个1排成一行,分隔成K个部分,也就是在N+K-1条分隔线中选K-1条,C[N+K-1][K-1]就是答案。 代码: #include #include #include using namespace

Baoli1008的专栏 579

uva10943 How do you add?(隔板法+递推)

题意:把一个数n分解成k个数相加的形式(允许有0)。 感受:本来以为组合数学题自己还是懂的一点点的,没想到啊!!高中组合数学都还给金进啦!一开始看了公式还是一头雾水(就很不爽),找了很久的解释,终于找到了详细的解释。智商仿佛在降低,看来要多补补了,哈哈哈哈哈哈哈! 大神说:很裸的隔板法。 引用一下维基上对隔板法的解释: 现在有10个球,要放进3个盒子里

onthewaytotop的博客 353

UVA 10943 How do you add?

Problem A: How do you add? Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ry

colorfulshark 1110

uva 10943 How do you add? (DP)

Problem E : How do you add? From:UVA, 10943 Problem A: How do you add? Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now s

不戚的专栏 690

UVA10943 How do you add【递推打表

Larry is very bad at math — he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a snowboarding accident.     They’re now trying to spend

海岛Blog 384

UVa 10943 - How do you add?

Description Larry is very bad at math - he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a sn...

weixin_30851867的博客 160
上一篇: Benefit(UVA 11889)
下一篇: Again Prime? No Time.(UVA 10780)
TryMyEdge
博客等级 码龄10年 0粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值