zoukankan      html  css  js  c++  java
  • HDU 4901 The Romantic Hero

    The Romantic Hero

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on HDU. Original ID: 4901
    64-bit integer IO format: %I64d      Java class name: Main
    There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

    You may wonder why this country has such an interesting tradition? It has a very long story, but I won't tell you :).

    Let us continue, the party princess's knight win the algorithm contest. When the devil hears about that, she decided to take some action.

    But before that, there is another party arose recently, the 'MengMengDa' party, everyone in this party feel everything is 'MengMengDa' and acts like a 'MengMengDa' guy.

    While they are very pleased about that, it brings many people in this kingdom troubles. So they decided to stop them.

    Our hero z*p come again, actually he is very good at Algorithm contest, so he invites the leader of the 'MengMengda' party xiaod*o to compete in an algorithm contest.

    As z*p is both handsome and talkative, he has many girl friends to deal with, on the contest day, he find he has 3 dating to complete and have no time to compete, so he let you to solve the problems for him.

    And the easiest problem in this contest is like that:

    There is n number a_1,a_2,...,a_n on the line. You can choose two set S(a_s1,a_s2,..,a_sk) and T(a_t1,a_t2,...,a_tm). Each element in S should be at the left of every element in T.(si < tj for all i,j). S and T shouldn't be empty.

    And what we want is the bitwise XOR of each element in S is equal to the bitwise AND of each element in T.

    How many ways are there to choose such two sets? You should output the result modulo 10^9+7.
     

    Input

    The first line contains an integer T, denoting the number of the test cases.
    For each test case, the first line contains a integers n.
    The next line contains n integers a_1,a_2,...,a_n which are separated by a single space.

    n<=103, 0 <= a_i <1024, T<=20.
     

    Output

    For each test case, output the result in one line.
     

    Sample Input

    2
    3
    1 2 3
    4
    1 2 3 3

    Sample Output

    1 
    4

    Source

     
    解题:动态规划
    由于有重复情况存在,需要强制选择划分的那个元素,所以有dp[i][j][0]表示前i个的异或和为j,且第i个不选的方案数
    dp[i][j][1]则表示前i个异或和为j且必选第i个的方案数
    dp2[i][j]表示从i开始,后面随便选几个与和为j的方案数
    那么答案就是
    [sum_{i=1}^{n-1}sum_{j=0}^{1023}dp[i][j][1]*dp2[i+1][j]]
     1 #include <bits/stdc++.h>
     2 using LL = long long;
     3 using namespace std;
     4 const int maxn = 1010;
     5 const LL mod = 1e9 + 7;
     6 LL dp[maxn][1024][2],dp2[maxn][1024];
     7 int n,a[maxn];
     8 int main(){
     9     int kase;
    10     scanf("%d",&kase);
    11     while(kase--){
    12         memset(dp,0,sizeof dp);
    13         memset(dp2,0,sizeof dp2);
    14         scanf("%d",&n);
    15         for(int i = 1; i <= n; ++i) scanf("%d",a + i);
    16         dp[1][a[1]][1] = 1;
    17         for(int i = 2; i <= n; ++i){
    18             dp[i][a[i]][1]++;
    19             for(int j = 0; j < 1024; ++j){
    20                 dp[i][j][0] += dp[i-1][j][0] + dp[i-1][j][1];
    21                 dp[i][j^a[i]][1] += dp[i-1][j][0] + dp[i-1][j][1];
    22                 dp[i][j][0] %= mod;
    23                 dp[i][j^a[i]][1] %= mod;
    24             }
    25         }
    26         dp2[n][a[n]] = 1;
    27         for(int i = n-1; i > 0; --i){
    28             dp2[i][a[i]]++;
    29             for(int j = 0; j < 1024; ++j){
    30                 dp2[i][j] += dp2[i+1][j];
    31                 dp2[i][j&a[i]] += dp2[i+1][j];
    32                 dp2[i][j] %= mod;
    33                 dp2[i][j&a[i]] %= mod;
    34             }
    35         }
    36        LL ret = 0;
    37        for(int i = 1; i < n; ++i)
    38         for(int j = 0; j < 1024; ++j)
    39             ret = (ret + dp[i][j][1]*dp2[i+1][j])%mod;
    40        printf("%I64d
    ",ret);
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    数据结构与算法(15)——冒泡法和选择法排序
    数据结构与算法(14)——二分查找算法
    数据结构与算法(13)—顺序查找法
    数据结构与算法(12)——动态规划案例
    Object Detection的一些进展(Valse2020.4.30)
    机器学习(1)——模型评估与选择
    数据结构与算法(12)—分治策略
    数据结构与算法(11)—递归
    数据结构与算法(10)——有序表OrderedList
    数据结构与算法(9)——无序表List
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4901401.html
Copyright © 2011-2022 走看看