确定的是多个可能的组合的数量来获得指定结果 [英] Determining the number of possible combinations of a number to get a specified result

查看:175
本文介绍了确定的是多个可能的组合的数量来获得指定结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样一个问题:

给定的整数,确定可能的组合仅使用2,3,7-数,其总和会给出整数

例如:

  4  -  2 {(2,2)}
9  -  3  -  {(2,7),(2,2,2,3),(3,3,3)}
 

一种方法是遍历3圈,然后再确定和是否是可以实现的。这里的code:

 为(i = 0; I< = NUM​​ / 2;我++){
    为(J = 0; J< = NUM​​ / 3; J ++){
         对于(K = 0; K< = NUM​​ / 7; k ++){
            如果(I * 2 + J * 3 + K * 7 = = NUM​​)
                 算上++;
}
 

下面计数将具有套可能的数目。但是,这是非常低效和需要O(N)的时间。我想知道是否有计算不同套数的任何其他有效的方式。

解决方案

一个DP的解决方案应该为这个问题应该是线性的。 (这里执行)

 的#include< stdio.h中>
#定义SZ 5
INT备忘录[SZ + 1 + 7]。


诠释的主要(无效){
    INT I = 0;
    memset的(安培;备忘录[0],0,sizeof的备忘录);
    备忘录[0] = 1;

    对于(i = 0; I< = SZ; ++ I)备注[I + 2] + =备忘录[I]
    对于(i = 0; I< = SZ; ++ I)备注[I + 3] + =备忘录[I]
    对于(i = 0; I< = SZ; ++ I)备注[I + 7 + =备忘录[I]

    的printf(%D \ N,备忘录[深圳]);

    返回0;
}
 

  1. 我们先从一维数组DP 备忘录与理想infinited大小 不引起出大小的(在实际中动态分配) 开往指数 SZ + MAX_NUM
  2. 在用来初始化元素 0 这个数组1,因为有1路 获得empty_sum。
  3. 如果我们能得到一些 K x中的方式,有x的方式来 获得 K + 2 K + 3 K + 7 。这是3环被使用。 (Number_of_ways [{2,3,7} + 1] + = number_of_ways [I])
  4. 在所有循环完成后,备忘录[K:0 - SZ]包含的数 方法我们可以得到氏/ STRONG>

给人一种复杂性为O(K * N),其中k为3这里(2,3,7)。为常数k,这是线性的。

I came across this question:

Given an integer, determine the number of possible combinations using only 2,3,7 whose sum will give the integer.

Eg:

4 - 2  {(2,2)}
9 - 3  {(2, 7), (2, 2, 2, 3), (3, 3, 3)}

One way is to iterate through 3 loops and then determine whether the sum is attainable. Here's the code:

for( i=0; i<=num/2; i++){
    for( j=0; j<=num/3; j++){
         for( k=0; k<=num/7; k++){
            if(i*2+j*3+k*7 == num) 
                 count++;
}

Here count will have the number of sets possible. But this is very inefficient and takes O(n3) time. I would like to know if there is any other efficient way of computing the number of different sets.

解决方案

A dp solution should for this problem should be linear. (Implemented here)

#include <stdio.h>
#define SZ 5
int memo[SZ+1+7];


int main(void) {
    int i = 0;
    memset(&memo[0], 0, sizeof memo);
    memo[0] = 1;

    for(i = 0; i <= SZ; ++i) memo[i+2] += memo[i];
    for(i = 0; i <= SZ; ++i) memo[i+3] += memo[i];
    for(i = 0; i <= SZ; ++i) memo[i+7] += memo[i];

    printf("%d\n", memo[SZ]);

    return 0;
}

  1. We start with a 1-D dp array memo with ideally infinited size (allocated dynamically in practice) of size which doesn't cause out of bound for index SZ + max_num.
  2. Initilize element 0 of this array with 1, because there is 1 way to obtain empty_sum.
  3. If we can obtain a number k in x ways, there are x more ways to obtain k+2, k+3 and k+7. This is what 3 loops are using. (Number_of_ways[{2,3,7}+i] += number_of_ways[i])
  4. After all loops are done, memo[k : 0 - SZ] contains the number of ways we can obtain k.

Giving a complexity of O(k * N) where k is 3 here (2, 3, 7). For constant k, this is linear.

这篇关于确定的是多个可能的组合的数量来获得指定结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆