题目链接:

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

E. Wet Shark and Blocks

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> 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 
> 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 10
> 3 5 6 7 8 9 5 1 1 1 1 5

样例输出

3

题意

给你n个数ai(ai>=1&&ai<=9),你每次要在其中选一个数,可以重复选,你现在要取b次,将选出来的数按选择的顺序组成一个b位的整数,现在问要使最后的结果%x==k,总共有多少种选法。

题解

dp[i][j]表示选出来的前i个数拼成的数%x==j的一共有多少种,则容易得到状态转移表达式:dp[i][(k10+j)%10]+=dp[i-1][k]cntv[j](cntv[j]表示n个数中等于j的有多少个)。

b有10^9,明显是需要矩阵加速一下!!!

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start----------------------------------------------------------------------
const int maxn=111;
const int mod=1e9+7; struct Matrix {
LL mat[maxn][maxn];
Matrix() { memset(mat, 0, sizeof(mat)); }
friend Matrix operator *(const Matrix& A, const Matrix& B);
friend Matrix operator +(const Matrix &A,const Matrix &B);
friend Matrix pow(Matrix A, int n);
}; Matrix I; Matrix operator +(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
ret.mat[i][j] = (A.mat[i][j] + B.mat[i][j])%mod;
}
}
return ret;
} Matrix operator *(const Matrix& A, const Matrix& B) {
Matrix ret;
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < maxn; k++) {
ret.mat[i][j] = (ret.mat[i][j]+A.mat[i][k] * B.mat[k][j]) % mod;
}
}
}
return ret;
} Matrix pow(Matrix A, int n) {
Matrix ret=I;
while (n) {
if (n & 1) ret = ret*A;
A = A*A;
n /= 2;
}
return ret;
} int n,m,k,mo;
LL cntv[11]; void solve(){
///状态转移矩阵
Matrix A;
for(int j=0;j<mo;j++){
for(int dig=1;dig<=9;dig++){
int i=(j*10+dig)%mo;
A.mat[i][j]+=cntv[dig];
}
}
///初始向量
Matrix vec;
for(int dig=1;dig<=9;dig++){
vec.mat[dig%mo][0]+=cntv[dig];
} vec=pow(A,m-1)*vec; prf("%I64d\n",vec.mat[k][0]); } void init(){
///单位矩阵
for(int i=0;i<maxn;i++) I.mat[i][i]=1;
clr(cntv,0);
} int main() {
init();
scf("%d%d%d%d",&n,&m,&k,&mo);
for(int i=1;i<=n;i++){
int x; scf("%d",&x);
cntv[x]++;
}
solve();
return 0;
} //end-----------------------------------------------------------------------

最新文章

  1. Javascrip的概述
  2. BusyBox Init
  3. jquery.validate.unobtrusive.js插件作用
  4. 【ruby】ruby基础知识
  5. 使用nodejs中httpProxy代理时候出现404异常
  6. hdu 5655 CA Loves Stick
  7. Ubuntu 14.04远程登录服务器--ssh的安装和配置
  8. 网站开发常用jQuery插件总结(十)菜单插件superfish
  9. codeforces 358D
  10. 【KMP】Oulipo
  11. Qt开发小工具之gif转换器(使用QMovie截取每一帧为QImage,然后用QFile另存为图片文件)
  12. ContentType是否大小写区分?
  13. asp.net小技巧:保留password模式文本框textbox内的数据不丢失。
  14. 第三章:基本HTML结构
  15. 【Android Developers Training】 34. 添加一个简单的分享行为(Action)
  16. JSP入门 分页
  17. (字符串 数组 递归 双指针) leetcode 344. Reverse String
  18. A1046. Shortest Distance
  19. 自动化部署iptables防火墙脚本
  20. Win10手记-IIS部署网站问题解决

热门文章

  1. Scala中的类学习
  2. FPGA之CORDIC算法实现_理论篇(上)
  3. net辅助工具列表
  4. Centos 6.4 安装mysql-5.6.14-linux-glibc2.5-i686.tar.gz
  5. 20155332 补交课后测试——ch11网络编程
  6. 【转载】OCX和DLL的区别
  7. 【LG3250】[HNOI2016]网络
  8. OpenStack入门篇(二十二)之实现阿里云VPC的SDN网络
  9. CentOS 下 SonarQube 6.7 的下载、配置、问题排查
  10. jenkins 多任务串行执行