Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
来源:互联网 发布:淘宝开店铺保证金多少 编辑:程序博客网 时间:2024/05/22 03:15
题意:
有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k。
分析:
dp+矩阵快速幂。
假如现在的数是m,模x余数是n,那么再从下一个block中选一个数a,a模x余数为b,那么新的数的余数就为
dp[i][(j * 10 + m) % x] = dp[i-1][j] * cnt[m];
可是
代码:
#include<cstdio>const int maxn = 50005;int cnt[maxn], r[maxn];const int N = 105, mod = 1e9 + 7;struct Matrix{ int row,cal; long long m[N][N];};Matrix init(Matrix a, long long t){ for(int i = 0; i < a.row; i++) for(int j = 0; j < a.cal; j++) a.m[i][j] = t; return a;}Matrix mul(Matrix a,Matrix b){ Matrix ans; ans.row = a.row, ans.cal = b.cal; ans = init(ans,0); for(int i = 0; i < a.row; i++) for(int j = 0; j < b.cal; j++) for(int k = 0; k < a.cal; k++) ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod; return ans;}long long quick_pow(int k, int x, int res, Matrix A){ Matrix I; I.row = x, I.cal = 1; I = init(I, 0); for(int i = 0; i < x; i++) I.m[i][0] = cnt[i]; while(k){ if(k&1) I = mul(A, I); A = mul(A, A); k>>=1; } return I.m[res][0]%mod;}int main (void){ int n, b, k, x; int a; scanf("%d%d%d%d",&n,&b,&k,&x); for(int i = 0; i < n; i++){ scanf("%d",&a); cnt[a%x]++; } for(int i = 0; i < x; i++) r[i] = (i *10)%x; Matrix t; t.row = t.cal = x; for(int i = 0; i < t.row; i++) for(int j = 0; j < t.cal; j++) t.m[i][j] = cnt[(i+x-r[j])%x]; printf("%I64d",quick_pow(b-1, x, k, t)); return 0;}
0 0
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- Codeforces 621E Wet Shark and Blocks【Dp+矩阵快速幂】
- CodeForces 621 E.Wet Shark and Blocks(dp+矩阵快速幂)
- Codeforces 621E 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 #341 E. Wet Shark and Blocks dp 矩阵优化
- Codeforces #341 div 2 E. Wet Shark and Blocks(矩阵快速幂)
- cf#341-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)★
- 【38.24%】【codeforces 621E】 Wet Shark and Blocks
- codeforces 621E Wet Shack and blocks dp+矩阵幂优化!
- 快速矩阵幂 Codeforces621E Wet Shark and Blocks
- Codeforces 621B Wet Shark and Bishops 【dp】
- Codeforces Round #341 (Div. 2) problemE Wet Shark and Blocks 矩阵乘法 dp
- Alien3D 第一部分 基本的調用代碼設計
- Gdi+ 编译不过的问题
- jquery杂记
- ubuntu 14.04 64位 编译C程序
- 备忘
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- Android-onInterceptTouchEvent()和onTouchEvent()总结
- Printf 输出格式
- iOS开发证书显示“此证书的签发者无效”的问题解决
- std - locale - global(...) 执行时机
- Missing iOS Distribution signing identity问题解决
- UTF8与GBK字符编码之间的相互转换
- 无源光网络的原理和构成
- 常见开源消息系统