首页 > 后端开发 > C++ > 正文

所有从1到n中取出的组合的乘积之和

WBOY
发布: 2023-09-06 16:01:06
转载
1339人浏览过

所有从1到n中取出的组合的乘积之和

如果从1到n逐个取数,可能会有多种组合。

例如,如果我们一次只取一个数字,组合的数量将为 nC1。

If we take two numbers at a time, the number of combinations will be nC2. Hence, the total number of combinations will be nC1 + nC2 +… + nCn.

要找到所有组合的总和,我们必须使用一种高效的方法。否则,时间和空间复杂度会变得非常高。

Problem Statement

找出从1到N中每次取出的数字的所有组合的乘积之和。

N is a given number.

示例

输入

有道小P
有道小P

有道小P,新一代AI全科学习助手,在学习中遇到任何问题都可以问我。

有道小P 64
查看详情 有道小P
N = 4
登录后复制

Output

f(1) = 10
f(2) = 35
f(3) = 50
f(4) = 24
登录后复制

Explanation

f(x) is the sum of the product of all combinations taken x at a time.
f(1) = 1 + 2+ 3+ 4 = 10
f(2) = (1*2) + (1*3) + (1*4) + (2*3) + (2*4) + (3*4) = 35
f(3) = (1*2*3) + (1*2*4) +(1*3*4) + (2*3*4) = 50
f(4) = (1*2*3*4) = 24 
登录后复制

输入

N = 5
登录后复制

Output

f(1) = 15
f(2) = 85
f(3) = 225
f(4) = 274
f(5) = 120
登录后复制

Brute Force Approach

暴力法是通过递归生成所有组合,找出它们的乘积,然后找到相应的和。

递归C++程序示例

下面是一个递归的C++程序,用于找到所有组合中(从1到N)每次取的乘积的和

#include <bits/stdc++.h>
using namespace std;
//sum of each combination
int sum = 0;
void create_combination(vector<int>vec, vector<int>combinations, int n, int r, int depth, int index) {
   // if we have reached sufficient depth
   if (index == r) {
      //find the product of the combination
    	int prod = 1;
    	for (int i = 0; i < r; i++)
    	prod = prod * combinations[i];
    	// add the product to sum
    	sum += prod;
    	return;
   }
   // recursion to produce a different combination
   for (int i = depth; i < n; i++) {
      combinations[index] = vec[i];
   	  create_combination(vec, combinations, n, r, i + 1, index + 1);
   }
}
//Function to print the sum of products of
//all combinations taken 1-N at a time
void get_combinations(vector<int>vec, int n) {
   for (int i = 1; i <= n; i++) {
      // vector for storing combination
         //int *combi = new int[i];
    	vector<int>combinations(i);
    	// call combination with r = i
    	// combination by taking i at a time
    	create_combination(vec, combinations, n, i, 0, 0);
    	// displaying sum of the product of combinations
    	cout << "f(" << i << ") = " << sum << endl;
        sum = 0;
    }
}
int main() {
   int n = 5;
   //creating vector of size n
   vector<int>vec(n);
   // storing numbers from 1-N in the vector
   for (int i = 0; i < n; i++)
   	vec[i] = i + 1;
   //Function call
   get_combinations(vec, n);
   return 0;
}
登录后复制

Output

f(1) = 15
f(2) = 85
f(3) = 225
f(4) = 274
f(5) = 120
登录后复制

By creating the recursion tree of this approach, it is visible that the time complexity is exponential. Also, many steps get repeated which makes the program redundant. Hence, it is highly inefficient.

Efficient Approach (Dynamic Programming)

An effective solution would be to use dynamic programming and remove the redundancies.

Dynamic programming is a technique in which a problem is divided into subproblems. The subproblems are solved, and their results are saved to avoid repetitions.

Example C++ Program using Dynamic Programming

Below is a C++ Program using Dynamic Programming to find the sum of all combinations taken (1 to N) at a time .

#include <bits/stdc++.h>
using namespace std;
//Function to find the postfix sum array
void postfix(int a[], int n) {
for (int i = n - 1; i > 0; i--)
   a[i - 1] = a[i - 1] + a[i];
}
//Function to store the previous results, so that the computations don't get repeated
void modify(int a[], int n) {
   for (int i = 1; i < n; i++)
      a[i - 1] = i * a[i];
}
//Function to find the sum of all combinations taken 1 to N at a time
void get_combinations(int a[], int n) {
   int sum = 0;
   // sum of combinations taken 1 at a time is simply the sum of the numbers 
   // from 1 - N
   for (int i = 1; i <= n; i++)
   	  sum += i;
   cout << "f(1) = " << sum <<endl;
      // Finding the sum of products for all combination
   for (int i = 1; i < n; i++) {
   	  //Function call to find the postfix array
   	  postfix(a, n - i + 1);
      // sum of products taken i+1 at a time
   	  sum = 0;
      for (int j = 1; j <= n - i; j++) {
         sum += (j * a[j]);
      }
      cout << "f(" << i + 1 << ") = " << sum <<endl;
      //Function call to modify the array for overlapping problem
      modify(a, n);
   }
}
int main() {
   int n = 5;
  int *a = new int[n];
   // storing numbers from 1 to N
   for (int i = 0; i < n; i++)
	  a[i] = i + 1;
   //Function call
   get_combinations(a, n);
   return 0;
}
登录后复制

Output

f(1) = 15
f(2) = 85
f(3) = 225
f(4) = 274
f(5) = 120
登录后复制

结论

在这篇文章中,我们讨论了找到从1到N中所有组合的乘积之和的问题。

我们从指数时间复杂度的暴力方法开始,然后使用动态规划进行了修改。同时还提供了两种方法的C++程序。

以上就是所有从1到n中取出的组合的乘积之和的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号