Problem Statement
Tak has NN cards. On the ii-th (1≤i≤N)(1≤i≤N) card is written an integer xixi. He is selecting one or more cards from these NN cards, so that the average of the integers written on the selected cards is exactly AA. In how many ways can he make his selection?
Constraints
- 1≤N≤501≤N≤50
- 1≤A≤501≤A≤50
- 1≤xi≤501≤xi≤50
- N,A,xiN,A,xi are integers.
Partial Score
- 200200 points will be awarded for passing the test set satisfying 1≤N≤161≤N≤16.
Input
The input is given from Standard Input in the following format:
NN AA
x1x1 x2x2 ...... xNxN
Output
Print the number of ways to select cards such that the average of the written integers is exactly AA.
Sample Input 1 Copy
Copy
4 8
7 9 8 9
Sample Output 1 Copy
Copy
5
- The following are the 55 ways to select cards such that the average is 88:
- Select the 33-rd card.
- Select the 11-st and 22-nd cards.
- Select the 11-st and 44-th cards.
- Select the 11-st, 22-nd and 33-rd cards.
- Select the 11-st, 33-rd and 44-th cards.
Sample Input 2 Copy
Copy
3 8
6 6 9
Sample Output 2 Copy
Copy
0
Sample Input 3 Copy
Copy
8 5
3 6 2 8 7 6 5 9
Sample Output 3 Copy
Copy
19
Sample Input 4 Copy
Copy
33 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Sample Output 4 Copy
Copy
8589934591
题意:
从n个数中选数(个数任意)求选出的这几个数平均数位k的种数;
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll f[60][60][6000];//f[i][j][k]前i个数选j个数和为k的种数。
ll a[60];
int sum[600]={};
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
f[0][0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
for(int k=sum[i];k>=0;k--)
{
f[i][j][k]+=f[i-1][j][k];
if(j>=1&&k>=a[i])
{
f[i][j][k]+=f[i-1][j-1][k-a[i]];
}
}
}
}
ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=f[n][i][m*i];
}
cout<<sum<<endl;
}