由前 n 个自然数组成的集合的所有子集的和
集合是数据元素的集合。集合的子集是由仅在父级集合后面的元素组成的集合。例如,如果 B 中的所有元素都在 A 中,那么 B 是 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
", ans); return 0; }
输出
The sim of the series is 2815
广告