麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Codeforces Round #341 (Div. 2)E(矩陣快速冪優(yōu)化dp,好題)

2019-11-14 10:20:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目鏈接E. Wet Shark and Blockstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Examplesinput
12 1 5 103 5 6 7 8 9 5 1 1 1 1 5output
3input
3 2 1 26 2 2output
0input
3 2 1 23 1 2output
6Note

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個(gè)數(shù),這n個(gè)數(shù)的大小都在1~9之間,有b塊集合,每個(gè)集合內(nèi)都有這n個(gè)數(shù),你要從每個(gè)集合中取出一個(gè)數(shù),并把它們依次拼接起來(lái)合并成一個(gè)大的整數(shù),問(wèn)最后這個(gè)整數(shù)%x得到k的方案數(shù)有多少。

題解:

我們可以先把1~9在n出現(xiàn)的次數(shù)用occ[i]存下來(lái) ,然后用dp[i][j]表示取前i個(gè)數(shù),最終模x后為j的方案數(shù),那么容易得到dp[0][0]=1,dp[i][j]=sum{dp[i-1][a]*occ[d] }(其中(a*10+d)%x==j),但因?yàn)閎太大,所以我們考慮用矩陣快速冪優(yōu)化.

由于對(duì)于指定的膜數(shù)x,我們可以遍歷每個(gè)余數(shù)i和j,再遍歷k(k為1-9的數(shù)),如果(i*10+k)%x==j,那么矩陣data[i][j]+=occ[k].

然后對(duì)構(gòu)造的這個(gè)矩陣進(jìn)行快速冪運(yùn)算即可。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=100+10;struct matrix{    int n;    ll data[maxn][maxn];    matrix(int tn=0){        n=tn;        rep(i,0,n) rep(j,0,n) data[i][j]=0;    }    void init(){        rep(i,0,n) data[i][i]=1;    }};matrix Operator *(matrix a,matrix b){    matrix c(a.n);    int n=a.n;    rep(i,0,n) rep(j,0,n) rep(k,0,n) c.data[i][j]=(c.data[i][j]+a.data[i][k]*b.data[k][j]%mod)%mod;    return c;}matrix func(matrix a,int b){    matrix t(a.n),ans(a.n);    ans.init();    t=a;    while(b)    {        if(b&1) ans=ans*t;        t=t*t;        b>>=1;    }    return ans;}int a[15];int main(){    int n,b,kk,x;    scanf("%d%d%d%d",&n,&b,&kk,&x);    rep(i,1,n+1)    {        int p;        scanf("%d",&p);        a[p]++;    }    matrix tmp(x);    rep(i,0,x)        rep(j,0,x)            rep(k,1,10)            if((i*10+k)%x==j) tmp.data[i][j]+=a[k]; //    matrix ans(x);    ans=func(tmp,b);    cout << ans.data[0][kk] << endl;    return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 19禁国产精品福利视频 | 欧美日韩视频在线播放 | 久久久久久久一区 | 久久精品亚洲国产奇米99 | 黄网站免费在线看 | 日本在线不卡免费 | 国产成人精品网站 | 午夜视频在线免费观看 | 中文字幕一二区 | 久久午夜免费视频 | 在线成人av观看 | 中国字幕av| 中国国语毛片免费观看视频 | 亚洲第一黄色网 | 久久久www成人免费精品 | 欧美日韩在线视频观看 | 色羞羞 | 久草在线看片 | 欧美日本一 | 在线观看av国产一区二区 | 亚洲免费视频一区二区 | 热99精品视频| 中国久久久 | 亚洲精品久久久久久 | 欧美18—19sex性护士中国 | 亚洲伊人色欲综合网 | 国产精品二区高清在线 | 精国产品一区二区三区四季综 | 国产精品一区二区羞羞答答 | 国产精品美女久久久免费 | 国产精品www | 91麻豆精品国产91久久久更新资源速度超快 | 免费在线观看亚洲 | 特片网久久 | 二区三区四区视频 | 久久人人做 | 国产毛片在线看 | 亚洲精品动漫在线观看 | 日本高清在线播放 | 高清视频一区二区 | 日本中文字幕久久 |