Happy Matt Friends (DP)

Source: Internet
Author: User

Happy Matt Friends

Time limit:6000/6000 MS (java/others) Memory limit:510000/510000 K (java/others)
Total submission (s): 418 Accepted Submission (s): 159


Problem Descriptionmatt has N friends. They is playing a game together.

Each of the Matt ' s friends has a magic number. In the game, Matt selects some (could is 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 the integers N, M (1≤n≤40, 0≤m≤106).

In the second line, there is N integers ki (0≤ki≤106), indicating the i-th friend ' s magic number.

Outputfor each test case, output A, "case #x: Y", where x was the case number (starting from 1) and Y indicates The number of ways where Matt can win.

Sample Input23 21 2 33 31 2 3

Sample outputcase #1:4Case #2:2HintIn the-RST sample, Matt can win by Selecting:friend with number 1 and friend with number 2. The XOR sum is a 3.friend with number 1 and a friend with number 3. The XOR sum is a 2.friend with number 2. The XOR sum is a 2.friend with number 3. The XOR sum is 3.  Hence, the answer is 4. Test instructions: Matt has n friends and now needs to select N (0<=n<=n) so that each friend's Ki value, xor or result (^) is greater than or equal to M.  Idea: First of all, pay attention to the topic data range: n<=40 m<=10^6 First rule out the situation of violent enumeration, DP thinking: Similar to the backpack: Consider the i+1 person, select or not select F[i][j] represents the former I, choose which K (k<=i), make the XOR result of J.   Recursive: Do not select the person I: f[i+1][j] + = f[i][j];     Here ans does not accumulate because both cases are in the same situation! Select I: f[i+1][j^a[i]] + = f[i][j]; At this point, if the current XOR result (J^a[i]) >=m, then add the final ans: ans + = f[i][j]; Note: The subject can also be calculated all F[i][j], using a double loop, SUM{F[N][J] (j>=m)}, which is the result. AC Code:
////main.cpp//20141104////Created by Songjs on 14-11-4.//Copyright (c) 2014 Songjs. All rights reserved.//#include<iostream>#include<stdio.h>#include<algorithm>#include<cstring>#include<string.h>#include<math.h>#include<queue>#include<stack>#include<stdlib.h>#include<map>using namespacestd;#defineLL Long Long#defineSF (a) scanf ("%d",& (a));#defineINF 2e9#defineINF 2147483647#defineN (1&LT;&LT;20)#definePI 3.141592653#defineEPS 1e-8#defineMAXC (1&LT;&LT;20)intf[ the][n];inta[ A];intMain () {intTintk=1; scanf ("%d",&T);  while(t--){        intn,m; scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) {scanf ("%d",&A[i]); } memset (F,0,sizeof(f)); f[0][0]=1; LL Num=0; if(m==0) num++;  for(intI=0; i<n;i++){             for(intj=0; j<maxc;j++)            {                //when not selected, the current selection does not change, and Num plus the corresponding case value is not required. f[i+1][J] + =F[i][j]; intt = j^A[i]; F[i+1][t] + =F[i][j]; if(t >= m) num+=f[i][j];//Note that this is an added F[I][J]!!! Add in every time! }} printf ("Case #%d:", k++); printf ("%i64d\n", num); }    return 0;}

For if(m==0) num++; Understanding: (Personal understanding, if not welcome everyone correction!!) )

When M is 0, a person is not selected, can be a case, plus this situation. (when M is not 0, you need to select at least 1 people.) )

Initialize f[0[0] = 1; Understanding:

The first 0 persons, XOR or result is 0 o'clock, for one case, the result is 1. (ie above)

At the same time this is because the double for loop is f[i+1][j every time it is added, so no repetition is counted!

 

Happy Matt Friends (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.