C - 高橋君とカード / Tak and Cards(dp)

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值