We need to compute sum of all elements of all subsets of the set S = {1, 2, ..., N}. That is, for each non-empty subset (including empty? The example includes only subsets with at least one element? Wait they listed all subsets including empty? Actually power set includes empty set. But in example they wrote: P(S) = {{1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}; they omitted empty set. Did they compute sum over all subsets except empty? Let's see: answer = (1)+(2)+(3)+(1+2)+(1+3)+(2+3)+(1+2+3) = 24. So they excluded empty subset sum (0). So sum over all non-empty subsets. However question statement: "sum of all elements of all the subsets in power set." Typically the empty subset has sum 0, so including it wouldn't change the answer. But they deliberately omitted it. So we need to clarify: Should we include empty set? In example they did not list it; but sum would be same if added 0. So maybe they intend to sum over all subsets including empty; but then they'd still get 24. So it's fine. But careful: if we compute sum over all subsets, empty subset contributes 0; so it's same as sum over non-empty. So we can compute sum over all subsets.
Now we need to compute for each test case N up to 42. T up to 42.
We can derive formula. Each element i from 1 to N appears in how many subsets? Total subsets = 2^N. For a given element i, it appears in exactly half of them: 2^{N-1} subsets. So sum of all elements across all subsets = sum_{i=1}^N i * 2^{N-1} = 2^{N-1} * (N(N+1)/2) = 2^{N-2} * N(N+1) if N >= 1? Wait careful: 2^{N-1} * (N(N+1)/2) = (N(N+1) * 2^{N-1}) / 2 = N(N+1) * 2^{N-2}. For N=1, 2^{N-2} = 2^{-1} = 0.5? That's not integer but sum must be integer. Actually for N=1, total subsets: { } and {1}. Sum of elements across both subsets = 1. Using formula: N=1 => N(N+1)=2, 2^{N-2}=2^{-1}=1/2 => product = 1, works as integer because 2^{N-2} is fraction but product yields integer because N(N+1) divisible by 2? Let's compute properly: sum = (N(N+1)/2) * 2^{N-1}. That's integer. For N=1: (12/2)=1 * 2^{0}=1 => 1. So we can use that. For N=0? Not in range. So formula: answer = (N(N+1)/2) * 2^{N-1}.
Check N=3: (3*4/2)=6, 2^{2}=4 => product = 24. Yes.
Thus problem reduces to compute that for each N.
But note constraints: N up to 42, so 2^{41} is about 2.2e12? Actually 2^10 = 1024 ~ 1e3, 2^20 ~ 1e6, 2^30 ~ 1e9, 2^40 ~ 1e12, 2^41 ~ 2e12, times N(N+1)/2 max ~ 903 (for N=42, 42*43/2=903). So product maximum ~ 903 * 2^41 ~ 903 * 2.2e12 = ~2e15. That's about 2.0e15, which fits within 64-bit signed integer (max ~9e18). So we can use 64-bit integers (long long in C++). Since T <= 42, no issue.
Thus answer is straightforward.
But wait: sample input: 1 test case, N=3, output 24. Yes.
So implementation: read T, for each N compute ans = (N*(N+1)/2) * (1LL << (N-1)). But careful: for N=1, 1<<0 = 1; (1*2/2)=1 => ans=1. Let's test manually: S={1}, subsets: {} sum=0, {1} sum=1 => total=1. Good.
However need to consider potential overflow if using 32-bit. Use 64-bit.
Also note that (1LL << (N-1)) for N up to 42 is fine, but if N=0 not allowed.
Alternatively compute using pow(2, N-1) but integer arithmetic.
Thus code in C++:
#include
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
long long sum = (N * (N + 1LL)) / 2; // to avoid overflow, use 64-bit multiplication.
long long ans = sum * (1LL << (N-1));
cout << ans << endl;
}
return 0;
}
Edge case: N=0? Not in constraints.
Thus problem solved.
But sometimes they might ask to output modulo something? Not here.
So answer.
Now, but we might need to output as per format: T lines.
Alright.
But wait, the problem states: "Power set of a set S is defined as set of all possible subsets of S." and "Set S consist of all the number from 1 to N." So S is {1,...,N}. Then "You need to calculate this sum for a given n." Possibly they want sum of all elements of all subsets (i.e., sum of sums). That's what we computed.
Thus final answer: for each N, compute (N(N+1)/2) * 2^{N-1}.
But to be thorough, we can also think of alternative combinatorial reasoning.
Now, we should provide solution.
Given that it's simple, we'll present code.
But caution: The constraints T up to 42, N up to 42 maybe they intend to compute using DP or precomputation? But formula works.
Thus final solution.
Now, let me produce final answer. ```cpp
#include
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
// sum of numbers from 1 to N
long long sumN = (N * (N + 1LL)) / 2;
// 2^(N-1)
long long power = 1LL << (N - 1);
long long ans = sumN * power;
cout << ans << endl;
}
return 0;
}