Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】
来源:互联网 发布:小气泡清洁的危害 知乎 编辑:程序博客网 时间:2024/05/22 00:05
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.
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.
Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.
12 1 5 103 5 6 7 8 9 5 1 1 1 1 5
3
3 2 1 26 2 2
0
3 2 1 23 1 2
6
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.
题意:有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;}
- Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】
- Codeforces #341 E. Wet Shark and Blocks dp 矩阵优化
- Codeforces 621E Wet Shark and Blocks【Dp+矩阵快速幂】
- CodeForces 621 E.Wet Shark and Blocks(dp+矩阵快速幂)
- Codeforces 621 E Wet Shark and Blocks
- CodeForces 621E Wet Shark and Blocks
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(dp + 矩阵快速幂)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(DP+矩阵快速幂)
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks(矩阵优化DP)★
- 【38.24%】【codeforces 621E】 Wet Shark and Blocks
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- Codeforces #341 div 2 E. Wet Shark and Blocks(矩阵快速幂)
- cf#341-E. Wet Shark and Blocks --DP+矩阵快速幂
- codeforces 621E Wet Shack and blocks dp+矩阵幂优化!
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks
- Linux下安装MySql(主备)
- 303. Range Sum Query - Immutable
- C# 中==和Equal的区别
- MTK平台Android4.4 拍照默认图片格式修改
- Jquery/js单元测试中实现Mock(js实现mock接口)
- Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】
- Android-WebView路由登录192.168.1.1
- 屏幕适配心得
- hdu 5461 Largest Point 【贪心】
- 深入分析JavaWeb 6 -- servletConfig 与servletContext详解
- 邻接矩阵存储有向图
- Android-Sql-数据性能优化
- 0和1中的最大子矩阵(1506加强版)(1505和2870)
- 136 socket-1 基础知识