程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 【矩陣乘法優化dp】[Codeforces 621E] Wet Shark and Blocks,codeforces621e

【矩陣乘法優化dp】[Codeforces 621E] Wet Shark and Blocks,codeforces621e

編輯:C++入門知識

【矩陣乘法優化dp】[Codeforces 621E] Wet Shark and Blocks,codeforces621e


http://codeforces.com/problemset/problem/621/E

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 chooseexactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit1 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, n, b, k 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.

 

題解

題目大意:n個數,每個數可重復多次選取,選b個數組成一個b位數,求這個數mod k=x的方案數,答案模10^9+7

dp[i][j]表示前i個數 mod x=j的方案數,cnt[i]表示i出現的次數,顯然dp[i][(i*10+j)%x]=dp[i-1][j]*cnt[j];

矩乘優化即可。

 

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define mod 1000000007
using namespace std;

int n,b,x,k;
int num[15];
struct matrix
{
    long long a[105][105];
    int n,m;
}ans,temp;
matrix mult(matrix,matrix);
matrix pow(matrix,int);

matrix pow(matrix a,int k)
{
    int i,j;
    matrix c;
    memset(c.a,(long long)0,sizeof(c.a));
    for(i=0;i<=x-1;i++) c.a[i][i]=(long long)1;
    while(k)
    {
        if(k&1) c=mult(a,c);
        a=mult(a,a);
        k>>=1;
    }
    return c;
}

matrix mult(matrix a,matrix b)
{
    int i,j,k;
    matrix c;
    memset(c.a,(long long)0,sizeof(c.a));
    for(i=0;i<=x-1;i++)
        for(j=0;j<=x-1;j++)
            for(k=0;k<=x-1;k++)
                c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
    return c;
}

int main()
{
    int i,j,t;
    scanf("%d%d%d%d",&n,&b,&k,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&t);
        num[t]++;
    }
    for(i=0;i<=x-1;i++)
        for(j=0;j<=9;j++)
             temp.a[i][(i*10+j)%x]=(temp.a[i][(i*10+j)%x]+(long long)num[j])%mod;
    ans=pow(temp,b);
    printf("%lld",ans.a[0][k]);
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved