HDU-5119 Happy Matt Friends
生活随笔
收集整理的這篇文章主要介紹了
HDU-5119 Happy Matt Friends
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? ? ? ? ? ?Happy Matt Friends Matt has N friends. They are playing a game together.?
Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.?
Matt wants to know the number of ways to win.
14年北京站題~
題目意思:給出t組數(shù)據(jù) n個數(shù) 求XOR出大于m的數(shù)的個數(shù),每個數(shù)都有兩種狀態(tài),取與不取。其實(shí)就是一個01背包。 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e6+10; const int MOD=1e9+7; ll a[maxn]; int dp[45][maxn],n,m; ll slove() {dp[1][0]=dp[1][a[1]]=1;for(int i=2; i<=n; i++){for(ll j=0; j<maxn; j++){dp[i][j]+=dp[i-1][j];dp[i][j^a[i]]+=dp[i-1][j];}}ll ans=0;for(ll i=m; i<maxn; i++){ans+=dp[n][i];}return ans; } int main() {int t,k=0;scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));scanf("%d %d",&n,&m);for(int i=1; i<=n; i++)scanf("%lld",&a[i]);ll ans=slove();printf("Case #%d: %lld\n",++k,ans);}return 0; }
Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.?
Matt wants to know the number of ways to win.
InputThe first line contains only one integer T , which indicates the number of test cases.?
For each test case, the first line contains two integers N, M (1 ≤ N ≤ 40, 0 ≤ M ≤ 10?6).?
In the second line, there are N integers ki (0 ≤ k?i?≤ 10?6), indicating the i-th friend’s magic number.OutputFor each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y indicates the number of ways where Matt can win.Sample Input
Sample Output
Case #1: 4 Case #2: 2Hint
In the ?rst sample, Matt can win by selecting: friend with number 1 and friend with number 2. The xor sum is 3. friend with number 1 and friend with number 3. The xor sum is 2. friend with number 2. The xor sum is 2. friend with number 3. The xor sum is 3. Hence, the answer is 4.14年北京站題~
題目意思:給出t組數(shù)據(jù) n個數(shù) 求XOR出大于m的數(shù)的個數(shù),每個數(shù)都有兩種狀態(tài),取與不取。其實(shí)就是一個01背包。 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e6+10; const int MOD=1e9+7; ll a[maxn]; int dp[45][maxn],n,m; ll slove() {dp[1][0]=dp[1][a[1]]=1;for(int i=2; i<=n; i++){for(ll j=0; j<maxn; j++){dp[i][j]+=dp[i-1][j];dp[i][j^a[i]]+=dp[i-1][j];}}ll ans=0;for(ll i=m; i<maxn; i++){ans+=dp[n][i];}return ans; } int main() {int t,k=0;scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));scanf("%d %d",&n,&m);for(int i=1; i<=n; i++)scanf("%lld",&a[i]);ll ans=slove();printf("Case #%d: %lld\n",++k,ans);}return 0; }
?
?
?
PS:摸魚怪的博客分享,歡迎感謝各路大牛的指點(diǎn)~
轉(zhuǎn)載于:https://www.cnblogs.com/MengX/p/9074603.html
總結(jié)
以上是生活随笔為你收集整理的HDU-5119 Happy Matt Friends的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery中click() 和oncl
- 下一篇: 团队作业—系统设计和任务分配