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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Happy Matt Friends(HDU5119 + dp)

發布時間:2025/4/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Happy Matt Friends(HDU5119 + dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:

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

題目:

題意:

  求選擇任意個數,使其異或和大于等于m的方案數。

思路:

  每個數有選和不選兩種方案,顯然是背包思想。dp[i][j]表示前i個物品異或和為j時的方案數,轉移方程為dp[i][j] = dp[i-1][j] + dp[i-1][j^a[i]]。這題可以考慮用滾動數組滾動掉一維,當然了,不滾動也是可以過滴~

代碼實現如下:

?

1 #include <set> 2 #include <map> 3 #include <deque> 4 #include <ctime> 5 #include <stack> 6 #include <cmath> 7 #include <queue> 8 #include <string> 9 #include <cstdio> 10 #include <vector> 11 #include <iomanip> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 17 typedef long long LL; 18 typedef pair<LL, LL> pll; 19 typedef pair<LL, int> pli; 20 typedef pair<int, int> pii; 21 typedef unsigned long long uLL; 22 23 #define lson rt<<1 24 #define rson rt<<1|1 25 #define name2str(name)(#name) 26 #define bug printf("**********\n"); 27 #define IO ios::sync_with_stdio(false); 28 #define debug(x) cout<<#x<<"=["<<x<<"]"<<endl; 29 #define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); 30 31 const double eps = 1e-8; 32 const int maxn = (1<<20) + 7; 33 const int inf = 0x3f3f3f3f; 34 const double pi = acos(-1.0); 35 const LL INF = 0x3f3f3f3f3f3f3f3fLL; 36 37 int t, n, m; 38 int a[45], dp[2][maxn]; 39 40 int main() { 41 #ifndef ONLINE_JUDGE 42 FIN; 43 #endif 44 int icase = 0; 45 scanf("%d", &t); 46 while(t--) { 47 scanf("%d%d", &n, &m); 48 int mx = 0, cnt = 0; 49 for(int i = 1; i <= n; i++) scanf("%d", &a[i]), mx = max(mx, a[i]); 50 memset(dp, 0, sizeof(dp)); 51 dp[0][0] = 1; 52 while(mx) cnt++,mx >>= 1; 53 for(int i = 1; i <= n; i++) { 54 for(int j = 0; j <= (1<<cnt); j++) { 55 dp[i&1][j] = dp[(i-1)&1][j] + dp[(i-1)&1][j^a[i]]; 56 } 57 } 58 LL ans = 0; 59 for(int i = m; i < maxn; i++) ans += dp[n&1][i]; 60 printf("Case #%d: %lld\n", ++icase, ans); 61 } 62 return 0; 63 }

?

轉載于:https://www.cnblogs.com/Dillonh/p/9747444.html

總結

以上是生活随笔為你收集整理的Happy Matt Friends(HDU5119 + dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。