https://community.topcoder.com/stat?c=problem_statement&pm=13015&rd=15846
https://codesea.wordpress.com/2014/03/31/a-easy-dp-problem-with-bitmask/
https://blog.csdn.net/stafuc/article/details/26636451
枚举每张卡片选或不选,复杂度250 ,考虑到限制1-10的数字可能重复出现,11-50的数字只会出现一次。因此在250 枚举中有很多重复的状态,把计算结果保留下来,使每种状态只计算一次,降低复杂度。每次枚举时保存一个值记录1-10的数字都有哪些出现过,另保存一个值记录11-50中的数字已经出现多少个了。
https://github.com/livingstonese/topcoder/blob/master/src/main/java/srm613/division2/TaroCards.java
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
https://codesea.wordpress.com/2014/03/31/a-easy-dp-problem-with-bitmask/
Given N integers. Let Total subsets T=2N. How many subsets are available from T where the distinct integers are less than or equal to K.
LL dp[59][1055];
LL a[59],n,k;
LL f(LL i,LL mask)
{
if(dp[i][mask]!=-1) return dp[i][mask];
if(i==n)
{
LL p=__builtin_popcount(mask);
if(p<=k) return 1;
else return 0;
}
LL res=f(i+1,mask); //not using i-th integer
LL nmask=mask;
nmask=nmask|(1<<a[i]);
res=res+f(i+1,nmask); //using i-th integer
return dp[i][mask]=res;
}
int main()
{
LL i,j,m,d;
while(cin>>n>>k)
{
memset(dp,-1,sizeof(dp));
for(i=0;i<n;i++)
cin>>a[i];
LL ans=f(0,0);
cout<<ans<<endl;
}
return 0;
}
https://blog.csdn.net/stafuc/article/details/26636451
枚举每张卡片选或不选,复杂度250 ,考虑到限制1-10的数字可能重复出现,11-50的数字只会出现一次。因此在250 枚举中有很多重复的状态,把计算结果保留下来,使每种状态只计算一次,降低复杂度。每次枚举时保存一个值记录1-10的数字都有哪些出现过,另保存一个值记录11-50中的数字已经出现多少个了。
https://github.com/livingstonese/topcoder/blob/master/src/main/java/srm613/division2/TaroCards.java
dp[i][sta][k]表示枚举到第i张卡,<=10的数字状态为sta,共有k个不同的数字的方案数,转移分第i张卡取或者不取
private static final int MAX_CARD_SET = 1024;
private static final int MAX_SINGLE_CARD_INTEGERS = 41;
private static final int MAX_CARDS = 51;
long[][][] dp = new long[MAX_CARD_SET][MAX_SINGLE_CARD_INTEGERS][MAX_CARDS];
boolean[][][] visited = new boolean[MAX_CARD_SET][MAX_SINGLE_CARD_INTEGERS][MAX_CARDS];
public long getNumber(int[] first, int[] second, int K) {
return getNumberUtil(first, second, K, 0, 0, 0);
}
private long getNumberUtil(int[] first, int[] second, int K, int S, int r, int i) {
if (visited[S][r][i])
return dp[S][r][i];
if (i == first.length) {
dp[S][r][i] = (Integer.bitCount(S) + r) <= K ? 1 : 0;
visited[S][r][i] = true;
return dp[S][r][i];
}
visited[S][r][i] = true;
// Don't pick card i
dp[S][r][i] = getNumberUtil(first, second, K, S, r, i+1);
// Pick card i
int S1 = S, r1=r;
if (first[i] < 11)
S1 = S1 | (1 << (first[i]-1));
else
r1++;
S1 = S1 | (1 << (second[i]-1));
dp[S][r][i] += getNumberUtil(first, second, K, S1, r1, i+1);
return dp[S][r][i];
}
LL dp[59][1055];
LL a[59],n,k;
LL f(LL i,LL mask)
{
if
(dp[i][mask]!=-1)
return
dp[i][mask];
if
(i==n)
{
LL p=__builtin_popcount(mask);
if
(p<=k)
return
1;
else
return
0;
}
LL res=f(i+1,mask);
//not using i-th integer
LL nmask=mask;
nmask=nmask|(1<<a[i]);
res=res+f(i+1,nmask);
//using i-th integer
return
dp[i][mask]=res;
}
int
main()
{
LL i,j,m,d;
while
(cin>>n>>k)
{
memset
(dp,-1,
sizeof
(dp));
for
(i=0;i<n;i++)
cin>>a[i];
LL ans=f(0,0);
cout<<ans<<endl;
}
return
0;
}