hustwinterC - Happy Matt Friends(dp解法)

來源:互聯網
上載者:User

標籤:

Description

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. 

Input

The 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. 

Output

For 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. 

  正解貌似是高斯消元....不會的說....PYY說不就是解方程麼....然後我去看了下.....挺多概念沒學線代的話還是挺眼生的...(orzpyy大神)記得高三的時候做過一個用矩陣加速求線性遞推式的東西....當時10^18的資料秒過....還是挺讓我驚訝了一下...
扯遠了... 這題dp也能做,有點類似01背包(dp真是夠渣,寒假看看能不能抽時間弄一下,依然是最大的短板)
只不過狀態轉移方程是 dp[i][j]=dp[i-1][j]+dp[i-1][j^a[i]]
然後正常些貌似會MLE。。。。。用到了滾動數組
其實滾動數組,完全...沒有理解的難度啊 
竟然是我第一次使用,大概是因為基本沒怎麼做過DP的題吧,而滾動數組這個最佳化貌似主要在Dp的題裡需要...
然後在搜滾動數組的時候,看到一篇部落格裡用異或來表示兩個狀態我覺得這一點也很贊.....
我自己想的話的大概就要又加,又mod,然後還得再來一個變數了吧.....差評滿滿
還有位元運算....是挺神的東西....目前還處於一知半解的階段....ORZ  M67大神

 

 1   2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 const long long C=1<<21; 7 long long dp[2][C]; 8 using namespace std; 9  10 int main()11 {12     int t,a[49];13     cin>>t;14     int tt;15     tt=t;16     while (t--)17  18     {19         int n,m,roll;20         roll=0;21          memset(dp,0,sizeof(dp));22         dp[0][0]=1;23         cin>>n>>m;24         for(int i=0;i<n;i++)25             cin>>a[i];26  27         for(int i=0;i<n;i++)28         {29             roll=roll^1;30             for(int j=0;j<=(C/2);j++)31                 dp[roll][j]=dp[roll^1][j]+dp[roll^1][j^a[i]];32         }33         long long ans=0;34         for(int i=m;i<=(C/2);i++)35             ans=ans+dp[roll][i];36             cout<<"Case #"<<tt-t<<": "<<ans<<endl;37  38     }39     return 0;40 }41  

 

  

hustwinterC - 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.