hdu 5119 Happy Matt Friends(dp)

来源:互联网 发布:电脑淘宝怎么上闲鱼 编辑:程序博客网 时间:2024/05/22 06:11

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5119

解题思路:

题目大意:

n个数,从中挑k个,使得这k个数的异或和不小于m,问有多少种挑选方法。

算法思想:

dp[i][j]表示前 i 个数中选择一些使得异或和为j的方法数,转移方程:dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]],即等于前i-1个异或和为j

的方法数加上前i-1个异或和为j ^ a[i]的方法数,因为j ^ a[i] ^ a[i] == j ^ (a[i] ^ a[i]) == j ^ 0 = j,所以最后再累计一下j大于等于m时

的方法数就行了。。。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;int const maxn = (1 << 20);ll dp[45][maxn];int a[45];int main(){    int T,t = 1;    scanf("%d", &T);    while(T--){        memset(dp,0,sizeof(dp));        dp[0][0] = 1;        int n,m;        scanf("%d %d",&n,&m);        for(int i = 1; i <= n; i++)            scanf("%d",&a[i]);        for(int i = 1; i <= n; i++)            for(int j = 0; j < maxn; j++)                dp[i][j] = dp[i - 1][j] + dp[i - 1][j ^ a[i]];        ll ans = 0;        for(int i = m; i < maxn; i++)            ans += dp[n][i];        printf("Case #%d: %lld\n",t++,ans);    }}


0 0
原创粉丝点击