首页 > 代码库 > [luoguP1373] 小a和uim之大逃离(DP)

[luoguP1373] 小a和uim之大逃离(DP)

传送门

 

题解

 

代码

#include <cstdio>#include <iostream>#define N 802#define mod 1000000007int n, m, p, ans;int a[N][N], f[N][N][16][2];inline int read(){	int x = 0, f = 1;	char ch = getchar();	for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;	return x * f;}int main(){	int i, j, k;	n = read();	m = read();	p = read() + 1;	for(i = 1; i <= n; i++)		for(j = 1; j <= m; j++)			f[i][j][(a[i][j] = read()) % p][0] = 1;	for(i = 1; i <= n; i++)		for(j = 1; j <= m; j++)		{			for(k = 0; k < p; k++)			{				f[i + 1][j][(k + a[i + 1][j]) % p][0] = (f[i + 1][j][(k + a[i + 1][j]) % p][0] + f[i][j][k][1]) % mod;				f[i + 1][j][(k - a[i + 1][j] + p) % p][1] = (f[i + 1][j][(k - a[i + 1][j] + p) % p][1] + f[i][j][k][0]) % mod;				f[i][j + 1][(k + a[i][j + 1]) % p][0] = (f[i][j + 1][(k + a[i][j + 1]) % p][0] + f[i][j][k][1]) % mod;				f[i][j + 1][(k - a[i][j + 1] + p) % p][1] = (f[i][j + 1][(k - a[i][j + 1] + p) % p][1] + f[i][j][k][0]) % mod;			}			ans = (ans + f[i][j][0][1]) % mod;		}	printf("%d\n", ans);	return 0;}

  

[luoguP1373] 小a和uim之大逃离(DP)