Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】

来源:互联网 发布:小气泡清洁的危害 知乎 编辑:程序博客网 时间:2024/05/22 00:05

E. Wet Shark and Blocks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 3 ways 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.

Sample test(s)
input
12 1 5 103 5 6 7 8 9 5 1 1 1 1 5
output
3
input
3 2 1 26 2 2
output
0
input
3 2 1 23 1 2
output
6
Note

In the second sample possible integers are 222662 and 66. None of them gives the remainder 1 modulo 2.

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


题意:有b组数,每组数均有n个且相同。你必须在每组选一个数,组成一个新数sum,使得sum % x == k,问方案数 % (1e9+7)。


思路:考虑dp。设置dp[i][j]表示前i个数所组成的数 % x == j的方案数。

转移有dp[i][(j*10+t)%x] = dp[i-1][j]。后面可以写成求和形式。转移相同,用矩阵加速。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 1000000#define eps 1e-8#define MAXN (200000+10)#define MAXM (100000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;typedef pair<int, int> pii;void add(LL &x, LL y){    x += y;    x %= MOD;}int n, b, k, X;struct Matrix{    LL a[110][110];};Matrix multi(Matrix x, Matrix y){    Matrix z; CLR(z.a, 0LL);    for(int i = 0; i < X; i++)    {        for(int k = 0; k < X; k++)        {            if(x.a[i][k] == 0) continue;            for(int j = 0; j < X; j++)                add(z.a[i][j], x.a[i][k] * y.a[k][j]);        }    }    return z;}Matrix Pow(Matrix x, int n){    Matrix y; CLR(y.a, 0LL);    for(int i = 0; i < X; i++) y.a[i][i] = 1LL;    while(n)    {        if(n & 1)            y = multi(x, y);        x = multi(x, x);        n >>= 1;    }    return y;}int cnt[10];int main(){    Ri(n); Ri(b); Ri(k); Ri(X);    Matrix ori, res; CLR(ori.a, 0LL); CLR(cnt, 0);    W(n)    {        int t; Ri(t);        cnt[t]++;    }    for(int i = 0; i < X; i++)        for(int j = 0; j <= 9; j++)            add(ori.a[i][(i*10+j)%X], 1LL*cnt[j]);    res = Pow(ori, b); Pl(res.a[0][k]);    return 0;}


0 0
原创粉丝点击