题目链接:

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-----------------------------------------------------------------------

Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks dp+矩阵加速的更多相关文章

  1. Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks

    题目大意:有m (m<=1e9) 个相同的块,每个块里边有n个数,每个数的范围是1-9,从每个块里边取出来一个数组成一个数,让你求组成的方案中 被x取模后,值为k的方案数.(1<=k< ...

  2. Codeforces Round #341 Div.2 C. Wet Shark and Flowers

    题意: 不概括了..太长了.. 额第一次做这种问题 算是概率dp吗? 保存前缀项中第一个和最后一个的概率 然后每添加新的一项 就解除前缀和第一项和最后一项的关系 并添加新的一项和保存的两项的关系 这里 ...

  3. Codeforces Round #341 Div.2 B. Wet Shark and Bishops

    题意:处在同一对角线上的主教(是这么翻译没错吧= =)会相互攻击 求互相攻击对数 由于有正负对角线 因此用两个数组分别保存每个主教写的 x-y 和 x+y 然后每个数组中扫描重复数字k ans加上kC ...

  4. Codeforces Round #341 Div.2 A. Wet Shark and Odd and Even

    题意是得到最大的偶数和 解决办法很简单 排个序 取和 如果是奇数就减去最小的奇数 #include <cstdio> #include <cmath> #include < ...

  5. Codeforces Round #341 (Div. 2)

    在家都变的懒惰了,好久没写题解了,补补CF 模拟 A - Wet Shark and Odd and Even #include <bits/stdc++.h> typedef long ...

  6. Codeforces Round #341 (Div. 2) ABCDE

    http://www.cnblogs.com/wenruo/p/5176375.html A. Wet Shark and Odd and Even 题意:输入n个数,选择其中任意个数,使和最大且为奇 ...

  7. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  8. Codeforces Round #341 (Div. 2) D. Rat Kwesh and Cheese 数学

    D. Rat Kwesh and Cheese 题目连接: http://www.codeforces.com/contest/621/problem/D Description Wet Shark ...

  9. Codeforces Round #341 (Div. 2)B

    B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. OVF3为订单原因分配成本中心时报错“成本中心未定义”,消息号:VT806

    问题:OVF3为订单原因分配成本中心时报错“成本中心未定义”,消息号:VT806.KS03检查成本中心数据是已经建立的. 原因:OVF3往右边拉动,还有一个需要填入的字段“有效起始日”,此字段值必须在 ...

  2. PhpStorm 回到上次编辑位置的快捷键

    回到上次编辑位置 Ctrl + Alt + <- (向后) Ctrl + Alt + -> (向前) 这个快捷键有时和电脑桌面快捷键冲突.解决办法: win + D 回到电脑桌面,右键-& ...

  3. updated stream stash changes

    处理的方式非常简单,主要是使用git stash命令进行处理,分成以下几个步骤进行处理. 1.先将本地修改存储起来 $ git stash 这样本地的所有修改就都被暂时存储起来 .是用git stas ...

  4. Gitlab+Jenkins学习之路(五)之git merge和git rebase的区别

    命令行测试 [root@linux-node1 ~]# mkdir testing [root@linux-node1 ~]# [root@linux-node1 ~]# cd testing/ [r ...

  5. 亿级PV请求的三种负载均衡技术

    在互联网+不断渗透到生活中的今天,各种各样的网络服务存在在我们身边,他们的访问流量也是大得惊人.一个大型网站(百万PV以上)想要正常访问,单单靠一台服务器是不可能提供稳定服务的.这时候就需要用负载均衡 ...

  6. [VB.NET][C#]二维向量的基本运算

    前言 在数学中,几何向量指具有大小(Magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念.向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 基于 ...

  7. 动态加载与插件系统的初步实现(二):AppDomain卸载与代理

    前一篇文章简单展示了类型发现和MEF使用,本文初步进入AppDomain相关内容. CLR程序运行时会创建默认程序集容器即AppDomain,默认AppDomain不支持卸载其程序集,但CLR支持创建 ...

  8. centos下安装docker,kubelet kubeadm kubectl

    目录 安装docker 安装命令 安装 kubelet kubeadm kubectl 安装命令 安装docker 安装命令 yum install docker -y 启动 systemctl en ...

  9. 使用TCP在同一台电脑上可以建立连接,在两台电脑上却连接失败的原因分析

    最近在用unity做联机游戏,在网络方面费了不少劲,总是在代码没问题的时候出一些莫名奇妙的BUG,不过后来都决定了.如果感觉代码没问题,八成就是防火墙的问题. 用unity发布后的游戏,如果涉及网络, ...

  10. 2019网易笔试题C++--丰收

    题目描述 又到了丰收的季节,恰好小易去牛牛的果园里游玩. 牛牛常说他多整个果园的每个地方都了如指掌,小易不太相信,所以他想考考牛牛. 在果园里有N堆苹果,每堆苹果的数量为ai,小易希望知道从左往右数第 ...