#P3713. [BJOI2017] 机动训练

    ID: 1352 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp搜索2017各省省选记忆化搜索

[BJOI2017] 机动训练

Description

Input the island’s terrain grid and compute the sum of weights over all mobility paths, where the weight of a mobility path is the number of mobility paths sharing the same terrain sequence. Output the result modulo 109+910^9+9.

Input Format

The first line contains two integers m,nm, n, the size of the island.

Then follow mm lines, each containing nn characters, describing the island’s terrain.

Output Format

Output a single number, the sum of weights over all mobility paths, modulo 109+910^9+9.

2 2
.*
*.
72
2 3
.*.
*.*
418
4 4
abba
baab
baab
abba
44512

Hint

Sample Explanation 1

Pairs in square brackets denote a mobility path, with coordinates in the order row, column:

  • Terrain sequence .*: $[(1, 1), (1, 2)],\ [(1, 1), (2, 1)],\ [(2, 2), (2, 1)],\ [(2, 2), (1, 2)]$, 44 paths in total, each with weight 44, contributing 1616.
  • Terrain sequence *.: $[(1, 2), (1, 1)],\ [(2, 1), (1, 1)],\ [(2, 1), (2, 2)],\ [(1, 2), (2, 2)]$, 44 paths in total, each with weight 44, contributing 1616.
  • Terrain sequence ..: [(1,1),(2,2)], [(2,2),(1,1)][(1, 1), (2, 2)],\ [(2, 2), (1, 1)], 22 paths in total, each with weight 22, contributing 44.
  • Terrain sequence **: [(1,2),(2,1)], [(2,1),(1,2)][(1, 2), (2, 1)],\ [(2, 1), (1, 2)], 22 paths in total, each with weight 22, contributing 44.
  • Terrain sequence .*.: $[(1, 1), (1, 2), (2, 2)],\ [(1, 1), (2, 1), (2, 2)],\ [(2, 2), (2, 1), (1, 1)],\ [(2, 2), (1, 2), (1, 1)]$, 44 paths in total, each with weight 44, contributing 1616.
  • Terrain sequence *.*: $[(1, 2), (1, 1), (2, 1)],\ [(2, 1), (1, 1), (1, 2)],\ [(1, 2), (2, 2), (2, 1)],\ [(2, 1), (2, 2), (1, 2)]$, 44 paths in total, each with weight 44, contributing 1616.

Total: 16+16+4+4+16+16=7216+16+4+4+16+16=72.

Sample Explanation 2

  • Terrain sequence .*: 77 paths, each with weight 77, contributing 4949.
  • Terrain sequence *.: 77 paths, each with weight 77, contributing 4949.
  • Terrain sequence ..: 44 paths, each with weight 44, contributing 1616.
  • Terrain sequence **: 44 paths, each with weight 44, contributing 1616.
  • Terrain sequence ..*: 22 paths, each with weight 22, contributing 44.
  • Terrain sequence .*.: 1010 paths, each with weight 1010, contributing 100100.
  • Terrain sequence .**: 22 paths, each with weight 22, contributing 44.
  • Terrain sequence *..: 22 paths, each with weight 22, contributing 44.
  • Terrain sequence *.*: 1010 paths, each with weight 1010, contributing 100100.
  • Terrain sequence **.: 22 paths, each with weight 22, contributing 44.
  • Terrain sequence .*.*: 66 paths, each with weight 66, contributing 3636.
  • Terrain sequence *.*.: 66 paths, each with weight 66, contributing 3636.

Total: 49+49+16+16+4+100+4+4+100+4+36+36=41849+49+16+16+4+100+4+4+100+4+36+36=418.

Constraints

  • For 10%10\% of the testdata, m×n4m\times n \le 4.
  • For 30%30\% of the testdata, m,n5m, n \le 5.
  • For 60%60\% of the testdata, m,n10m, n \le 10.
  • In an additional 20%20\% of the testdata, all terrains are the same.
  • For 100%100\% of the testdata, 1m,n301 \le m, n \le 30, and the character set consists of uppercase letters, lowercase letters, digits, and . *.

Translated by ChatGPT 5