[JZOJ5398]: Adore (shape memory pressure DP + search)

Title Description

  Small $ w $ accidentally saw a $ DAG $.
  This has $ $ $ m $ the DAG layer, only one source point of the first layer, the last layer is only a sink, each of the remaining layer has $ k $ nodes.
  Now small $ $ W can invert each connected between the first $ i (1 <i <n-1 ) $ layer and $ i + 1 $ layer side. I.e. from the original $ (i, k_1) $ connected to the $ (i + 1, k_2) $ edges becomes from $ (i, k_2) $ connected to the $ (i + 1, k_1) $.
  I ask him how many negated program, the number of paths from source to sink into an even number?
  The answer modulo $ 998 244 353 $.


Input Format

  Line two integers $ m, k $.
  Next $ m-1 $ row, the first row and the last row has $ k $ integer $ 0 $ or $ 1 $, the rest of each line has $ k ^ 2 $ integer $ 0 $ or $ 1 $, the $ (J- 1) \ times k + t $ represents integers $ (i, j) $ to $ (i + 1, t) $ has no edges.


Output Format

  A row of integer answer.


Sample

Sample input:

5 3
1 0 1
0 1 0 1 1 0 0 0 1
0 1 1 1 0 0 0 1 1
0 1 1

Sample output:

4


Data range and tips

  $ 20 \% $ data satisfies $ n \ leqslant 10, k \ leqslant 2 $.
  $ 40 \% $ data satisfies $ n \ leqslant 10 ^ 3, k \ leqslant 2 $.
  $ 60 \% $ data satisfies $ m \ leqslant 10 ^ 3, k \ leqslant 5 $.
  $ 100 \ $% of the data satisfies $ 4 \ leqslant m \ leqslant 10 ^ 4, k \ leqslant 10 $.


answer

$ K $ found little, considering the DP-shaped pressure $ $, set $ dp [i] [s] $ $ I $ indicates the status of the line, the edge points can be connected to the program number of $ s $.

Transfer with a memory search to search from back to front.

Time complexity: $ \ Theta (NK2 ^ K) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int M,K,S;
int Map[2][10001][11],a[11],g[1025];
long long dp[10001][1025];
int lowbit(int x){return x&-x;}
long long dfs(int x,int s)
{
	if(dp[x][s]!=-1)return dp[x][s];
	if(x==2)
	{
		dp[x][s]=1;
		for(int i=1;i<=K;i++)dp[x][s]^=a[i]&((s&(1<<(i-1)))!=0);
	}
	else
	{
		int ls=0,rs=0;
		for(int i=1;i<=K;i++)
		{
			ls|=g[Map[0][x-1][i]&s]<<(i-1);
			rs|=g[Map[1][x-1][i]&s]<<(i-1);
		}
		dp[x][s]=(dfs(x-1,ls)+dfs(x-1,rs))%mod;
	}
	return dp[x][s];
}
int main()
{
	memset(dp,-1,sizeof(dp));
	scanf("%d%d",&M,&K);
	for(int i=1;i<(1<<K);i++)g[i]=g[i-lowbit(i)]^1;
	for(int i=1;i<=K;i++)scanf("%d",&a[i]);
	for(int i=2;i<M-1;i++)
		for(int j=1;j<=K;j++)
			for(int k=1;k<=K;k++)
			{
				int x;scanf("%d",&x);
				Map[0][i][j]|=x<<(k-1);
				Map[1][i][k]|=x<<(j-1);
			}
	for(int i=1;i<=K;i++)
	{
		int x;
		scanf("%d",&x);
		S|=x<<(i-1);
	}
	printf("%lld",dfs(M-1,S));
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11832842.html