UVA 10943 HOW do you add?

本文介绍了一个有趣的数学问题:如何计算K个数相加等于N的所有可能组合数量。使用动态规划解决这一问题,提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question;
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 some time figuring out some good
problems, and Ryan will eat Larry if he cannot answer, so his fate
is up to you!
It’s a very simple problem — given a number N, how many ways
can K numbers less than N add up to N?
For example, for N = 20 and K = 2, there are 21 ways:
0+20
1+19
2+18
3+17
4+16
5+15

18+2
19+1
20+0
Input
Each line will contain a pair of numbers N and K. N and K will both be an integer from 1 to 100,
inclusive. The input will terminate on 2 0’s.
Output
Since Larry is only interested in the last few digits of the answer, for each pair of numbers N and K,
print a single number mod 1,000,000 on a single line.
Sample Input
20 2
20 2
0 0
Sample Output
21
21
**提议概述:给你一个数N和K,让你将N拆分为K个数,并让你求出有多少种拆分的方法?
思路:这道题看到的时候,开始感觉挺忙然,但你会发现这道题用dp是不错的选择;
核心:dp[N][K]=dp[i][k-1] (0<=i<=N)**
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/B)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[105][105];
void inti()
{
    memset(dp,0,sizeof(dp));
    int i,j,k;
    for(i=0;i<105;i++)
        dp[0][i]=1;
    for(k=1;k<105;k++)
    {
        for(j=1;j<105;j++)
        {
            for(i=0;i<=j;i++)
                dp[j][k]=(dp[j][k]+dp[i][k-1])%1000000;
        }

    }
}
int main()
{
    int n,k;
    inti();
    while (~scanf("%d%d",&n,&k))
    {
        if(n==0&&k==0)
            break;
        printf("%d\n",dp[n][k]);
    }
    return 0;
}

体会:遇到此类问题应先考虑dp会使问题变得简便!

UVA11076 Add Again问题的大意如下: 给定一组整数,其中可能包含重复的数字。需要将这些数字的所有不同排列组合中的每一个数值相加,然后计算这个总和。 要解决此类问题,可以通过以下方法进行分析与实现: - 首先统计每个数字出现的次数,利用组合数学的知识来避免生成所有排列。 - 对于每一位(个位、十位、百位等),计算该位上每个数字对总和的贡献。具体来说,如果共有 $k$ 个不同的排列,那么每个数字在每一位上出现的次数为 $k / n$,其中 $n$ 是数字的总数。 - 每一位的总贡献等于该位上所有数字的值乘以其出现次数之和,再乘以对应的位置权值(如个位是1,十位是10,百位是100等)。 例如,对于输入数据组 `[1, 2, 3]`: - 所有不同的排列是:[123, 132, 213, 231, 312, 321]。 - 总和为 `123 + 132 + 213 + 231 + 312 + 321 = 1332`。 具体的实现代码如下: ```python from math import factorial from collections import Counter def solve(): numbers = list(map(int, input().split())) count = len(numbers) # 统计每个数字的出现次数 freq = Counter(numbers) # 计算所有排列的数量 total_permutations = factorial(count) for v in freq.values(): total_permutations //= factorial(v) # 计算每个位置的贡献 sum_of_digits = sum(numbers) contribution_per_digit = total_permutations // count # 计算最终结果 result = 0 position_value = 1 for _ in range(count): digit_sum = sum_of_digits * contribution_per_digit result += digit_sum * position_value position_value *= 10 print(result) # 调用solve函数运行逻辑 solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值