Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks
2016-02-03 09:52
302 查看
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 3ways 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.
Sample test(s)
input
output
input
output
input
output
Note
题意:给你n个数,这n个数的大小都在1~9之间,有b块集合,每个集合内都有这n个数,你要从每个集合中取出一个数,并把它们依次拼接起来合并成一个大的整数,问最后这个整数%x得到k的方案数有多少。
思路:我们可以先把1~9在n出现的次数用occ[i]存下来 ,然后用dp[i][j]表示取前i个数,最终模x后为j的方案数,那么容易得到dp[0][0]=1,dp[i][j]=sum{dp[i-1][a]*occ[d] }(其中(a*10+d)%x==k),但因为b太大,所以我们考虑用矩阵快速幂优化.
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 3ways 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.
Sample test(s)
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
题意:给你n个数,这n个数的大小都在1~9之间,有b块集合,每个集合内都有这n个数,你要从每个集合中取出一个数,并把它们依次拼接起来合并成一个大的整数,问最后这个整数%x得到k的方案数有多少。
思路:我们可以先把1~9在n出现的次数用occ[i]存下来 ,然后用dp[i][j]表示取前i个数,最终模x后为j的方案数,那么容易得到dp[0][0]=1,dp[i][j]=sum{dp[i-1][a]*occ[d] }(其中(a*10+d)%x==k),但因为b太大,所以我们考虑用矩阵快速幂优化.
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; typedef __int64 ll; #define inf 99999999 #define pi acos(-1.0) int a[50050],occ[20]; #define MOD 1000000007 struct matrix{ ll n,m,i; ll data[105][105]; void init_danwei(){ for(i=0;i<n;i++){ data[i][i]=1; } } }; matrix multi(matrix &a,matrix &b){ ll i,j,k; matrix temp; temp.n=a.n; temp.m=b.m; for(i=0;i<temp.n;i++){ for(j=0;j<temp.m;j++){ temp.data[i][j]=0; } } for(i=0;i<a.n;i++){ for(k=0;k<a.m;k++){ if(a.data[i][k]>0){ for(j=0;j<b.m;j++){ temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD; } } } } return temp; } matrix fast_mod(matrix &a,ll n){ matrix ans; ans.n=a.n; ans.m=a.m; memset(ans.data,0,sizeof(ans.data)); ans.init_danwei(); while(n>0){ if(n&1)ans=multi(ans,a); a=multi(a,a); n>>=1; } return ans; } int main() { int n,m,i,j,b,k,x,d; while(scanf("%d%d%d%d",&n,&b,&k,&x)!=EOF) { memset(occ,0,sizeof(occ)); for(i=1;i<=n;i++){ scanf("%d",&a[i]); occ[a[i] ]++; } matrix A; A.n=x;A.m=1; memset(A.data,0,sizeof(A.data)); A.data[0][0]=1; matrix B; B.n=B.m=x; memset(B.data,0,sizeof(B.data)); for(i=0;i<x;i++){ for(j=0;j<x;j++){ for(d=1;d<=9;d++){ if( (j*10+d)%x==i ){ B.data[i][j]+=occ[d]; } } } } matrix ant; ant=fast_mod(B,b); matrix ans; ans=multi(ant,A); printf("%I64d\n",ans.data[k][0]); } return 0; }
相关文章推荐
- opencv2中代码在opencv3下运行时报错的修改一些方法
- mysql查询优化器为什么可能会选择错误的执行计划
- Android应用开发基础篇(7)-----BroadcastReceiver
- Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法
- 一些其他信息的整理
- 自定义选择框和选择开关
- JuCheap V2.0响应式后台管理系统模板正式发布beta版本
- 2016蓝桥杯基础训练——字母图形
- 实现对ibatis原生SQL的拦截改造-可以实现物理分页等(咋个办呢 zgbn)
- InitializeObjectAttributes 【MSDN翻译】
- C++/MFC修行之路(1) 文件操作
- EXCEL:CONCATENATE函数 设置格式
- 新品如何快速打造成爆款
- OpenCms学习(一)安装小记
- Core Animation实例-图片折叠效果(CAGradientLayer渐变层)
- TestFlight被收购了,那我们怎么使用呢?
- git 相关的技术
- Android Sqlite数据库操作
- Android应用开发基础篇(6)-----Service
- 集合框架