We need to count number of subsets (non-empty) of the N cards such that the average of the selected numbers equals A. Equivalent: sum of selected numbers = A * (size of subset). Let k be number of selected cards, and sum s. Condition: s = A * k.
We need count of subsets (non-empty) with this property. N up to 50, values up to 50, A up to 50.
Brute force over all subsets is 2^50 impossible. But we can use DP. This is a classic DP problem: counting subsets with given sum and number of elements. Since both N and values are small (max sum = 50*50=2500), we can do DP over number of items and sum.
Let dp[i][j][s] = number of ways to select j cards from first i cards with total sum s. Then answer = sum over j>=1 of dp[N][j][A*j].
Complexities: i up to 50, j up to 50, s up to 2500. dp size 51512501 ~ 6.5e6, which is fine. However we need to be careful with memory; we can use 2D DP for each j? Alternatively use 2D dp for each j? But typical approach: dp[j][s] updating from i. Use 2D array [cards count][sum] and iterate over items, updating backwards to avoid reuse. Since N=50, sum max 2500, j max 50, dp[51][2501] ~ 127,551, times maybe two for loops? Actually we need dp[j][s] dimension: (0..N) x (0..max_sum). But max_sum = Nmax_x = 2500. So dp size ~ 512501 ≈ 127,551, which is small. But wait, we need to count subsets of any size. But we need to count subsets with sum = A * k. But dp[j][s] counts number of subsets of exactly j cards with sum s. So we can compute dp iteratively.
Initialize dp[0][0]=1. For each card value v, iterate j from current max down to 0, and s from current max sum down to 0, to update: dp[j+1][s+v] += dp[j][s].
We need to ensure we don't overflow. The answer can be large: up to 2^50 - 1 ≈ 1e15, which fits in 64-bit. But some DP values may also be up to 2^50, so we need 64-bit integer (long long). In example 4, answer is 8589934591 which is about 2^33? Actually 2^33 = 8589934592, minus 1 = 8589934591, so it's large but within 64-bit.
Thus we can do that.
Alternative: DP using map to reduce states, but N small.
So implement:
- Read N, A, list x.
- max_sum = N * 50 (or sum of x). But we can compute max_sum = N*50 = 2500.
- dp = array of (N+1) x (max_sum+1) long long, initialize 0.
- dp[0][0] = 1.
- For each v in x:
for j from i down to 0: (where i is current processed count?) Actually we need to iterate j from current maximum number of cards downwards to avoid using same card multiple times. Since we are adding one card, we should iterate j from N-1 down to 0, and s from max_sum - v down to 0: dp[j+1][s+v] += dp[j][s].
- After processing all cards, answer = sum_{j=1..N} dp[j][Aj] (if Aj <= max_sum). Note that A may be up to 50, j up to 50, so A*j up to 2500, okay.
Edge cases: Ensure indices within bounds.
That should work.
But wait, there is nuance: The DP approach counts subsets, not sequences, and the order doesn't matter. Since we process each card once and update dp in reverse order, we correctly count each subset exactly once.
Now test with given examples.
Example 1: N=4, A=8, x=[7,9,8,9].
We compute:
dp after all:
- subsets of size 1: sums 7,9,8,9 -> none equal to 8? Actually 8 exists, so dp[1][8]=1? Wait there is an 8 card, so yes dp[1][8]=1.
- size 2: subsets: (7,9) sum=16 average 8 -> counts? 16=2*8 => dp[2][16] should count. (7,8)=15 not 16, (7,9)=16 again? Actually there are two 9's: indices: 2 and 4. So subsets: (7,9a) sum=16, (7,9b)=16, (9a,8)=17, (9a,9b)=18, (8,9b)=17. So dp[2][16]=2? But both (7,9a) and (7,9b) are distinct subsets because cards are distinct even if values same. So count 2.
- size 3: subsets: (7,9,8) sum=24 avg=8 => 24=3*8 => dp[3][24]=? Subsets: (7,9a,8) sum=24, (7,9b,8)=24, (9a,9b,8)=26, (7,9a,9b)=25. So dp[3][24]=2.
- size 4: (7,9a,8,9b) sum=33 avg=8.25 -> not 8; (all) sum=33 not 32. So dp[4][32]=0.
Total = 1+2+2 =5. Matches output.
Example 2: N=3 A=8 x=[6,6,9]
Compute subsets:
size1: 6,6,9 none 8.
size2: (6,6)=12 avg6, (6,9)=15 avg7.5, (6,9 other)=15 -> none 16? Actually sum needed 16 for avg8. So none.
size3: sum=21 avg7 -> no.
Answer 0.
Example 3: N=8 A=5 x=[3,6,2,8,7,6,5,9] Output 19. We'll trust.
Example 4: N=33 A=3, all cards 3. Then any non-empty subset has average 3 because all values are 3. So number of non-empty subsets = 2^33 - 1 = 8589934591. So DP should produce that.
Thus DP solution works.
Implementation details:
- Use vector<vector> dp(N+1, vector(max_sum+1, 0));
- Iterate: for each v in x:
for j from N-1 down to 0:
for s from max_sum - v down to 0:
if dp[j][s] > 0: dp[j+1][s+v] += dp[j][s];
But we can always update even if zero, but skip loops if zero? Actually we can do unconditional addition; it's fine because zeros add nothing. But we must ensure we don't overflow indices: s loop down to 0, s+v <= max_sum, safe. Also j+1 <= N.
We can also limit max_sum to the actual sum of all numbers to reduce loops, but not needed.
Now careful about loops order: Since we are updating dp in place and going backwards in j and s, we avoid using the same card multiple times. That's correct.
Potential optimization: we can keep track of current maximum sum reached so far, but not needed.
Complexity: O(N * N * max_sum) worst-case? Actually loops: for each v (N), for j from current max down to 0 (N), for s from max_sum down to 0 (max_sum). That's O(N^2 * max_sum) = 50502500 = 6.25e6, fine.
But we can reduce inner loop to only up to current max_sum_so_far, but again fine.
Implement and answer.
Edge: max_sum = N * 50, but if some x_i > 50? constraints say ≤50.
Now memory: dp size (51 x 2501) ~ 127,551 longs, ~ 1 MB.
Thus solution is straightforward.
Now we need to print answer as integer.
Let's implement in C++.
Pseudo:
#include
#include
using namespace std;
typedef long long ll;
int main() {
int N, A;
cin >> N >> A;
vector x(N);
for (int i=0; i<N; ++i) cin >> x[i];
int max_sum = N * 50; // or accumulate, but this is safe.
vector<vector> dp(N+1, vector(max_sum+1, 0));
dp[0][0] = 1;
for (int i=0; i<N; ++i) {
int v = x[i];
for (int j = N-1; j >= 0; --j) {
for (int s = max_sum - v; s >= 0; --s) {
if (dp[j][s]) {
dp[j+1][s+v] += dp[j][s];
}
}
}
}
ll ans = 0;
for (int j = 1; j <= N; ++j) {
int target = A * j;
if (target <= max_sum) {
ans += dp[j][target];
}
}
cout << ans << endl;
return 0;
}
But wait: max_sum = N50 = 2500; but if some x_i are less, it's fine. However, we should use maybe sum of all x_i as max_sum to be safe? Actually the maximum sum we can achieve is sum of all x_i, which could be less than N50 if numbers are smaller. But we allocate up to N50 anyway, that's fine. However, the DP loops go up to max_sum - v, but if max_sum is larger than actual needed, dp entries beyond actual sum remain zero; it's okay. But we must ensure that when we update, s+v may be up to N50, which is within allocated.
But we have to be careful: if we set max_sum to N*50, and v is up to 50, then s loop from max_sum-v down to 0, that's 2500-50=2450 downwards, okay.
But we also need to ensure dp dimension for s up to max_sum inclusive, so max_sum=2500, indices 0..2500 inclusive, size 2501. When we update dp[j+1][s+v], s+v can be up to max_sum (since s <= max_sum - v). So okay.
Alternatively we could compute total sum = sum of x_i, and use that as max_sum, but it's not necessary.
Now test: Example 4: N=33, max_sum=3350=1650, but actual sum = 333=99, much less. DP will update many zero entries? That's okay, loops still O(NNmax_sum) = 33331650 ≈ 1.8 million, fine.
But we might have to be careful about integer overflow: dp values may become up to 2^50 ~ 1e15, but long long (64-bit) max is 9e18, so okay.
Now answer: output as long long.
Thus solution is done.
But the problem might be from AtCoder Beginner Contest "Average" perhaps? Actually it's typical DP. I recall an AtCoder problem: "DP: average". Yes.
Potential improvements: use DP with offset for negative? Not needed.
Thus final answer: code as above.
One more check: Should we include empty set? The problem says "selecting one or more cards", so we exclude empty set. So we start from j=1.
Thus solution complete.
Now we can provide explanation.
In the analysis, we need to output code. So we'll output C++.
Let's also note: There might be alternative DP using sum - A*k, but this is similar.
Thus final. ```cpp
#include
#include
using namespace std;
typedef long long ll;
int main() {
int N, A;
cin >> N >> A;
vector x(N);
for (int i = 0; i < N; ++i) cin >> x[i];
// maximum possible sum (all cards are 50)
const int MAX_SUM = N * 50;
// dp[j][s] = number of ways to choose exactly j cards with total sum s
vector<vector<ll>> dp(N + 1, vector<ll>(MAX_SUM + 1, 0));
dp[0][0] = 1;
for (int v : x) {
// go backwards to avoid using the same card more than once
for (int j = N - 1; j >= 0; --j) {
for (int s = MAX_SUM - v; s >= 0; --s) {
if (dp[j][s]) {
dp[j + 1][s + v] += dp[j][s];
}
}
}
}
ll ans = 0;
for (int j = 1; j <= N; ++j) {
int target = A * j;
if (target <= MAX_SUM) {
ans += dp[j][target];
}
}
cout << ans << endl;
return 0;
}