由前n个自然数组成的集合的所有子集的总和
集合是数据元素的集合。集的子集是仅由父集之后的元素形成的集。例如,如果B的所有元素都存在于A中,则B是a的A子集。
在这里,我们需要找到由前n个自然数找到的集合的所有子集的总和。这意味着我需要找到所有可以形成的子集,然后添加它们。让我们举个例子
N=3
集合={1,2,3}
形成的子集={{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3,}}
总和=1+1+2+1+3+2+2+3+3+1+2+3=24
让我们重新排列总和1+1+1+1+2+2+2+2+3+3+3=4(1+2+3)=24
该类型的级数存在一个数学公式,该级数的通用公式为2^n*(n^2+n+2)–1。
示例
#include <stdio.h>
#define mod (int)(1e9 + 7)
int power(int x, int y) {
int res = 1;
x = x % mod;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int main() {
int n = 45;
n--;
int ans = n * n;
if (ans >= mod)
ans %= mod;
ans += n + 2;
if (ans >= mod)
ans %= mod;
ans = (power(2, n) % mod * ans % mod) % mod;
ans = (ans - 1 + mod) % mod;
printf("The sum of the series is %d \n", ans);
return 0;
}输出结果
The sim of the series is 2815