HDU-5119 Happy Matt Friends (背包DP,遞推枚舉)

來源:互聯網
上載者:User

標籤:就是   friend   遞推   amp   friends   滾動   return   end   數組   

題意:n個物品,第i個物品的權值為ki,選出一些物品使它們的異或不小於m,求有多少種方案

資料範圍:1 <= n <= 40,0 <= m <= 1e6

思路:其實就是換了一種要求的背包,MX要開得比1e6大一些,不滾動數組也能過去。

老套路設dp[i][j]為前i個物品異或為j時的方案,dp[i][j] = dp[i-1][j] + dp[i-1][j^a[i]],最後把dp[n][m以上]的值求和

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define LL long long 5 using namespace std; 6  7 const int mx = 1<<20; 8 int a[50]; 9 LL dp[50][mx+10];10 11 int main(){12     int t, kase = 0;13     scanf("%d", &t);14     while (t--){15         int n, m;16         memset(dp, 0, sizeof dp);17         LL ans = 0;18         scanf("%d%d", &n, &m);19         for (int i = 1; i <= n; i++)20             scanf("%lld", &a[i]);21         dp[0][0] = 1;22         for (int i = 1; i <= n; i++)23             for (int j = 0; j <= mx; j++)24                 dp[i][j] = dp[i-1][j] + dp[i-1][j^a[i]];25         for (int i = m; i < mx; i++)26             ans += dp[n][i];27         printf("Case #%d: %lld\n", ++kase, ans);28     }29     return 0;30 }

 

HDU-5119 Happy Matt Friends (背包DP,遞推枚舉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.