日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

高橋君とカード / Tak and Cards(AtCoder-2037)

發(fā)布時間:2025/3/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高橋君とカード / Tak and Cards(AtCoder-2037) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Problem Description

Tak has N cards. On the i-th (1≤i≤N) card is written an integer xi. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?

Constraints

1≤N≤50
1≤A≤50
1≤xi≤50
N,?A,?xi are integers

Partial Score

200 points will be awarded for passing the test set satisfying 1≤N≤16.

Input

The input is given from Standard Input in the following format:

N A
x1 x2 … xN

Output

Print the number of ways to select cards such that the average of the written integers is exactly A.

Example

Sample Input 1

4 8
7 9 8 9

Sample Output 1

5
The following are the 5 ways to select cards such that the average is 8:
Select the 3-rd card.
Select the 1-st and 2-nd cards.
Select the 1-st and 4-th cards.
Select the 1-st, 2-nd and 3-rd cards.
Select the 1-st, 3-rd and 4-th cards.

Sample Input 2

3 8
6 6 9

Sample Output 2

0

Sample Input 3

8 5
3 6 2 8 7 6 5 9

Sample Output 3

19

Sample Input 4

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

8589934591
The answer may not fit into a 32-bit integer.

題意:有 n 張牌,每張牌有一個整數(shù) xi,現(xiàn)在要在 n 張牌里選 1 或多張,使得所選的牌的平均值是 A,問有多少種選擇方式

思路:

由于所給的數(shù)據(jù)范圍不大,因此可設(shè) dp[i][j] 為前 i 張卡上的數(shù)相加為 j 的方案數(shù)

那么首先枚舉?n 件物品,每次考慮使得取得的卡數(shù) i?減少 1 張,那么相應(yīng)的和的最大值有 j+x[k],因此,對于前 i+1 張卡其相加的值為 j+x[k],那么組成的方案數(shù) dp[i+1][j+x[k]] 就為原本的方案數(shù)再加上前 i 張卡的值,即:dp[i+1][j+x[k]]+=dp[i][j]

最后統(tǒng)計方案數(shù)即可

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 50+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std; int x[N]; LL dp[N][N*N];//dp[i][j]表示前i個數(shù)相加值為j的方案數(shù) int main(){int n,a;scanf("%d%d",&n,&a);for(int i=1;i<=n;i++)scanf("%d",&x[i]);dp[0][0]=1;for(int k=1;k<=n;k++){//n個物品for(int i=k-1;i>=0;i--){//每次取得的物品個數(shù)減少for(int j=0;j<=50*i;j++){//統(tǒng)計相加的值,最大為50*idp[i+1][j+x[k]]+=dp[i][j];}}}LL res=0;for(int i=1;i<=n;i++){int val=i*a;//選擇i個物品時他們平均值的總和res+=dp[i][val];}printf("%lld\n",res);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的高橋君とカード / Tak and Cards(AtCoder-2037)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。