由前 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP