[Title] brush sequence problem

In the face of this title

 

 

 then

 

plan 1:

Bare, N ^ 3

#include<cstdio>
#include<cstdlib>
using namespace std;
int n;
const int N=103,M=1024;
int d[N];

int back[N][M];
void get_back(int nw,int pre,int x)
{
    for(int i=0;i<1024;i++)
        if(back[pre][i])
            back[nw][i]+=back[pre][i],back[nw][i&x]+=back[pre][i];
    back[nw][x]++;
}

int front[N][M];
void get_front(int nw,int pre,int x)
{
    for(int i=0;i<1024;i++)
    {
        for(int j=pre;j;j--)    
            front[nw][i^x]+=front[j][i];    
    }
    front[nw][x]++;
}

long long ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&d[i]);
    
    for(int i=n-1;i;i--)
        get_back(i,i+1,d[i+1]);
    for(int i=1;i<n;i++)
        get_front(i,i-1,d[i]);
    
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<1024;j++)
            if(front[i][j]) ans+=back[i][j]*front[i][j];
    }
    printf("%lld\n",ans);
    
    return 0;
}

Option II:

DP, solution really interesting

An important discovery:
two numbers is equal, or two different numbers equal to 0

Therefore, the problem can be simplified to
find a sequence of length n, selecting at least two numbers,
the number of set a1, a2, a3 ... ai ... aj
make a1 ^ a2 ^ a3 ^ ... ^ ai & ai + 1 & ... aj
result is 0

Here is an enumeration reduction i,
and are enumerated a complexity of n * 1024

#include <cstdio> 
#include <the cstdlib>
 the using  namespace STD;
 int n-;
 const  int N = 1003 ;
 int D [N];
 Long  Long F [N] [ 1024 ] [ 2 ]; // This indicates that 0,1 operation once performed & ^ 

int main () 
{ 
    Scanf ( " % D " , & n-);
     for ( int I = . 1 ; I <= n-; I ++) Scanf ( " % D " , & D [I]); 
    
    F [n-] [D [n-]] [ 0 ] = . 1; 
    for(int i=n-1;i;i--)
    {    
        for(int j=0;j<1024;j++)
        {
            f[i][j][0]+=f[i+1][j][0];
            f[i][j][1]+=f[i+1][j][1];
            
            f[i][j^d[i] ][1] +=f[i+1][j][1]+f[i+1][j][0]; 
            f[i][j&d[i] ][0] +=f[i+1][j][0];
        }
        f[i][d[i]][0]++; 
    }
    
    printf("%lld\n",f[1][0][1]);
    return 0;
}

 

Estimate the range of answers, particularly large,

(Although I do not actually estimate)

Anyway, definitely add burst, you need pressure level precision

At the meeting code, etc.

Guess you like

Origin www.cnblogs.com/xwww666666/p/11432027.html