HDU 4901 the romantic hero (DP)

Source: Internet
Author: User

Link: HDU 4901 the romantic hero

Given a sequence, two sets, S and T must be selected, and the subscript of the element selected in S must be smaller than the subscript of the element in T. In addition, the sum of elements in S must be equal to the sum of elements in T.

Problem-solving ideas: DP, the idea is very good, that is, the details are disgusting.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef __int64 ll;const int maxn = 1024;const ll MOD = 1e9+7;int n, num[maxn];ll l[maxn][maxn+10][2], r[maxn][maxn+10];void init () {    scanf("%d", &n);    for (int i = 1; i <= n; i++)        scanf("%d", &num[i]);}ll solve () {    memset(l, 0, sizeof(l));    memset(r, 0, sizeof(r));    for (int i = 1; i < n; i++) {        l[i][num[i]][1]++;        for (int j = 0; j < maxn; j++) {            l[i+1][j][1] = (l[i][j^num[i+1]][1] + l[i][j^num[i+1]][0])  % MOD;            l[i+1][j][0] = (l[i][j][1] + l[i][j][0]) % MOD;        }    }    for (int i = n; i; i--) {        r[i][num[i]]++;        for (int j = 0; j < maxn; j++) {            r[i-1][j] = (r[i-1][j] + r[i][j]) % MOD;            r[i-1][j&num[i-1]] = (r[i-1][j&num[i-1]] + r[i][j]) % MOD;        }    }    ll ans = 0;    for (int i = 1; i < n; i++) {        for (int j = 0; j < maxn; j++) {            /*            if (l[i][j] * r[i+1][j])                printf("%d %d %lld %lld\n", i, j, l[i][j], r[i+1][j]);                */            ans = (ans + l[i][j][1] * r[i+1][j] % MOD) % MOD;        }    }    return ans;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        init();        printf("%I64d\n", solve());    }    return 0;}

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.