HDU 5119 Happy Matt Friends (2014 Beijing Regional Live race H-question Bare backpack DP)

Source: Internet
Author: User

Although it is a simple DP, not even the rolling array can be AC, the amount of data is not very large.

For the number of N, there are only two states for each number, taking and not taking.

It is easy to derive the state transition equation:

Dp[i][j] = dp[i-1][j ^ A[i]] + dp[i-1][j];

DP[I][J] means that for the first I number in the sequence, the XOR and the number of schemes that happen to be J

Dp[i-1][j in the state transition equation] means that the current number is not taken, Dp[i-1][j ^ A[i]] indicates that the current number is to be taken.

This problem is still to understand Ah!

Source code:

//#pragma COMMENT (linker, "/stack:16777216")//For C + + Compiler#include <stdio.h>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<algorithm>#definell Long Long#defineMax (b) ((a) > (b))? (a): (b))#defineMin (b) ((a) < (a))? (a): (b))#defineAbs (x) (((x) > 0)? (x): (-(x)))using namespacestd;Const intINF =0x3f3f3f3f;intdp[2][1<< -];inta[ A];intMain () {intI, J, K, T, N, m, numcase =0; scanf ("%d",&T);  while(t--) {scanf ("%d%d",&n,&m);  for(i =1; I <= N; ++i) scanf ("%d", A +i); Memset (DP,0,sizeof(DP)); dp[0][0] =1;  for(i =1; I <= N; ++i) {             for(j =0; J < (1<< -); ++j) {Dp[i%2][J] = dp[(i-1) %2][j ^ A[i]] + dp[(i-1) %2][j]; }        }        Long LongAns =0;  for(i = m; I < (1<< -); ++i) {ans+ = dp[n%2][i]; } printf ("Case #%d:%i64d\n",++numcase, ans); }    return 0;}

HDU 5119 Happy Matt Friends (2014 Beijing Regional Live race H-question Bare backpack 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.