矩阵乘法&&dp加速矩阵的思路(E. Wet Shark and Blocks)

Posted Heilce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了矩阵乘法&&dp加速矩阵的思路(E. Wet Shark and Blocks)相关的知识,希望对你有一定的参考价值。

There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second block, he gets the integer 12.

Wet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 109?+?7.

Note, that the number of ways to choose some digit in the block is equal to the number of it‘s occurrences. For example, there are 3ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5.

Input

The first line of the input contains four space-separated integers, nbk and x (2?≤?n?≤?50?000,?1?≤?b?≤?109,?0?≤?k?<?x?≤?100,?x?≥?2) — the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself.

The next line contains n space separated integers ai (1?≤?ai?≤?9), that give the digits contained in each block.

Output

Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.

Examples
input
12 1 5 10
3 5 6 7 8 9 5 1 1 1 1 5
output
3
input
3 2 1 2
6 2 2
output
0
input
3 2 1 2
3 1 2
output
6
Note

In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.

In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.

 

1、这个问题要是想到dp的话还是很容易想到状态的转移方程的,但是时间复杂度太高我们需要优化。

2、有一种矩阵优化的方式,这个题目一看b的范围那么打我们能想到的就是快速幂,但是不能纯粹的快速幂,我们需要加上矩阵的优化。

3、但是如何构造矩阵成了本题的关键,我看了某大牛的思路感觉这个很好。http://codeforces.com/blog/entry/23196

 

 

以上是关于矩阵乘法&&dp加速矩阵的思路(E. Wet Shark and Blocks)的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)

BZOJ 1009--GT考试(KMP&DP&矩阵乘法)

UVA - 348Optimal Array Multiplication Sequence(递推)

LuoguP4719 模板动态 DP(树形DP,矩阵加速,LCT)

[BZOJ1009] [HNOI2008] GT考试 (KMP & dp & 矩阵乘法)

矩阵乘法加速图上问题专题总结