2014 + school fourth game 1005 | HDU 4901 the romantic hero (DP)

Source: Internet
Author: User

Question Link

Question: Let's give you a series, let you select a number to form a set S, and pick another number to form a set T, it is required that the subscript of each number in S must be smaller than that of T in the original sequence. The values of all values in s are the same as those in T, and you can find out how many combinations meet the requirements.

Idea: there was no clue during the competition. Later, the second brother came up with the state transition equation, and YN changed a lot of details and finally got. In short, it is an awkward DP .....

The first is _ XOR [I] [J ^ A [I] + = _ XOR [I-1] [J]; the next state of J is an exclusive or upper A [I], this array indicates that a set composed of certain numbers (including I) is selected in the first I number, and J ^ A [I] is obtained.

However, this will be repeated, because there is no obvious line between the selection of S and T elements before and after the subscript. It is found to be the same in the hatchback. Then we reset an array to store all the previous values:

_ XOR [I] [J ^ A [I] + = xort [I-1] [J];
Xort [I] [J] = _ XOR [I] [J] + xort [I-1] [J]; // this expression, indicates that some numbers are selected for the first I (I must be selected) when the value after the XOR is J, add the first I-1 to select the value after the XOR is J. In fact, it is to add the two cases of selecting and not selecting I, which is composed of some numbers in the former I, or J. At this time, I can be optional.

 1 //4901 2 #include <cstring> 3 #include <cstdio> 4 #include <iostream> 5 typedef long long  LL ; 6 const long long mod = 1000000007 ; 7 using namespace std ; 8  9 int a[1050] ;10 LL _xor[1050][1050],xort[1050][1050],_and[1050][1050],andt[1050][1050] ;11 12 void Init()13 {14     memset(_xor,0,sizeof(_xor)) ;15     memset(xort,0,sizeof(xort)) ;16     memset(_and,0,sizeof(_and)) ;17     memset(andt,0,sizeof(andt)) ;18     memset(a,0,sizeof(a)) ;19 }20 int main()21 {22     int T,n ;23     cin >> T ;24     while(T -- )25     {26         cin >> n ;27         Init() ;28         for(int i = 0 ; i < n ; i++)29             cin >> a[i] ;30         _xor[0][a[0]] = xort[0][a[0]] = 1 ;31         for(int i = 1 ; i < n ; i++)32         {33             for(int j = 0 ; j < 1024 ; j ++)34             {35                 _xor[i][j^a[i]] += xort[i-1][j] ;36                  _xor[i][j^a[i]]  %= mod ;37             }38             _xor[i][a[i]] ++ ;39             for(int j = 0 ; j < 1024 ; j++)40             {41                 xort[i][j] = _xor[i][j] + xort[i-1][j] ;42                 xort[i][j] %= mod ;43             }44         }45         _and[n-1][a[n-1]] = andt[n-1][a[n-1]] = 1 ;46         for(int i = n-2 ; i >= 0 ; i--)47         {48             for(int j = 0 ; j < 1024 ; j++)49             {50                 _and[i][j&a[i]] += andt[i+1][j] ;51                 _and[i][j&a[i]] %= mod ;52             }53             _and[i][a[i]] ++ ;54             for(int j = 0 ; j < 1024 ; j++){55                 andt[i][j] = andt[i+1][j] + _and[i][j] ;56                 andt[i][j] %= mod ;57             }58         }59         LL ans = 0 ;60         for(int i = 0 ; i < n-1 ; i++)61         {62             for(int j = 0 ; j < 1024 ; j++)63             {64                 ans += (_xor[i][j] * andt[i+1][j]) % mod ;65                 ans %= mod ;66             }67         }68         cout << ans << endl ;69      }70      return 0 ;71 }
View code

 

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.