[AtCoder] C - Tak and Cards
传送门:https://atcoder.jp/contests/abc044/tasks/arc060_a
挺容易想到的dp,但是因为脑残改了一晚上,最后出错的地方自己都看笑了
//数据很小,复杂度最高但最好理解的三维dp也能过
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<algorithm> 6 #include<cmath> 7 #include<string> 8 #include<stack> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 //#include<windows.h> 13 #define fi first 14 #define se second 15 #define pb(i) push_back(i) 16 #define rep(i,a,b) for(int i=a;i<=b;i++) 17 #define per(i,a,b) for(int i=a;i>=b;i--) 18 #define mes(a,b) memset(a,b,sizeof(a)) 19 using namespace std; 20 typedef long long ll; 21 typedef unsigned long long ull; 22 const int N = 55+10; 23 const int INF = 0x3f3f3f3f; 24 const int inf = - INF; 25 const int mod = 1e9+7; 26 const double pi = acos(-1.0); 27 28 ll x[N]; 29 ll dp[N][N][N*N]; 30 int main(){ 31 int n,a; 32 cin>>n>>a; 33 mes(dp,0); 34 rep(i,1,n) 35 cin>>x[i]; 36 rep(i,0,n){ 37 rep(j,0,n){ 38 rep(k,0,n*a){//i->目前到第几张牌 j->取了几张牌 k->总和 39 if(i==0&&j==0&&k==0) dp[i][j][k]=1; 40 else if(i>=1&&k<x[i]) dp[i][j][k]=dp[i-1][j][k]; 41 else if(i>=1&&j>=1&&k>=x[i]) dp[i][j][k]=dp[i-1][j][k]+dp[i-1][j-1][k-x[i]]; 42 else dp[i][j][k]=0; 43 } 44 } 45 } 46 ll ans=0; 47 rep(i,1,n){//最后竟然出错在这里,让i从0开始遍历了。 48 ans+=dp[n][i][i*a]; 49 } 50 cout<<ans<<endl; 51 //system("pause"); 52 return 0; 53 }