Problem Statement
Tak has <var>N</var> cards. On the <var>i</var>-th <var>(1≤i≤N)</var> card is written an integer <var>xi</var>. He is selecting one or more cards from these <var>N</var> cards, so that the average of the integers written on the selected cards is exactly <var>A</var>. In how many ways can he make his selection?
Constraints
- <var>1≤N≤50</var>
- <var>1≤A≤50</var>
- <var>1≤xi≤50</var>
- <var>N, A, xi</var> are integers.
Partial Score
- <var>200</var> points will be awarded for passing the test set satisfying <var>1≤N≤16</var>.
Input
The input is given from Standard Input in the following format:
<var>N</var> <var>A</var> <var>x1</var> <var>x2</var> <var>…</var> <var>xN</var>
Output
Print the number of ways to select cards such that the average of the written integers is exactly <var>A</var>.
Sample Input 1
4 8 7 9 8 9
Sample Output 1
5
- The following are the <var>5</var> ways to select cards such that the average is <var>8</var>:
- Select the <var>3</var>-rd card.
- Select the <var>1</var>-st and <var>2</var>-nd cards.
- Select the <var>1</var>-st and <var>4</var>-th cards.
- Select the <var>1</var>-st, <var>2</var>-nd and <var>3</var>-rd cards.
- Select the <var>1</var>-st, <var>3</var>-rd and <var>4</var>-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 <var>32</var>-bit integer.
题意:
给了你N个数,和一个数a,
让你从这N个数中选择出一些数,并使这些数的sum和是a的倍数。
问你有多少种方案数。
思路:
我们定义dp[i][j] 代表选了i个数时,sum和为j的方案数。
初始化dp[0][0]=1
转移方程时dp[i+1][j+x]+=dp[i][j],x为输入的数。
细节见代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll dp[55][55*55]; int k; int n; int main() { //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); gbtb; cin>>n>>k; int x; dp[0][0]=1ll; repd(i,1,n) { cin>>x; for(int j=i-1;j>=0;j--)// 枚举i-1~0, 一定倒序枚举,以避免重复相加。 { for(int w=0;w<=55*j;w++)// 枚举选了j个数的所有可能得到的值。 { dp[j+1][w+x]+=dp[j][w]; // 加上贡献。 } } } ll ans=0ll; repd(i,1,n) { ans+=dp[i][i*k];// 根据平均数的公式计算答案。 } cout<<ans<<endl; return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }