【38.24%】【codeforces 621E】 Wet Shark and Blocks
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
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.
Examples
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
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个数字;
让你从每个盒子中都取出一个数字1..9;
顺序组成长度为b的数字;
问数字取余结果为k的数字个数;
预处理出每个数字有多少个;
如果b没那么大的话可以这样写;
设dp[i][j]表示前i个数字组成的数取余结果为j的方案数
dp[i+1][(j*10+t)%k]+=dp[i][j]*num[t];
对于每一个转移;
其实都是(j*10+t)%k += t这个数字的个数*dp[i][j];
即每个转移都是一样的;
则一开始预处理出一个初始矩阵a[i][j];
这个矩阵表示的是
一开始余数为i的时候利用1-9这几个数字到余数为j的方案数增加量;
(还记得图论的从某个点到另外一个点恰好走k步的方案吗;http://blog.csdn.net/harlow_cheng/article/details/52615106我们一开始的初始矩阵也是任意两个点之间能否到达->即从i点到j点的方案增加量,(我们一开始是设为1表示联通的,那不正是方案增加量吗?)),这里也可以看成是知道任意两点之间(0..x-1)->(0..x-1)的连通关系,但是初始的时候任意两点联通对方案的增加量变成了一个可能大于1的数字);
求矩阵的b次幂;
最后的那个矩阵是为了加深理解;可以不用乘;->矩阵E->从0->0一开始的方案都为1,即都不变;
乘一下就能够和答案联系在一起了;
即从一开始取余为0->…->k
最后输出ans[0][k];
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long
using namespace std;
const int MAXMOD = 120;
const LL MOD = 1e9+7;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
struct abc
{
LL jz[MAXMOD][MAXMOD];
};
LL num[10];
int n,b,k,x;
LL f[MAXMOD];
abc a;
void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
abc jc(abc a, abc b)
{
abc c;
for (int i = 0; i <= x-1; i++)
for (int j = 0; j <= x-1; j++)
{
c.jz[i][j] = 0;
for (int k = 0; k <= x-1; k++)
c.jz[i][j] = (c.jz[i][j] + a.jz[i][k] * b.jz[k][j])%MOD;
}
return c;
}
abc ksm(int x)
{
if (x == 1)
return a;
abc temp;
temp = ksm(x>>1);
temp = jc(temp,temp);
if (x&1)
temp = jc(temp,a);
return temp;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);input_int(b);input_int(k);input_int(x);
for (int i = 1;i <= n;i++)
{
int temp;
input_int(temp);
num[temp]++;
}
int now = 0;
for (int i = 0;i<=x-1;i++)
for (int j = 1;j <= 9;j++)
a.jz[i][(i*10+j)%x]=(a.jz[i][(i*10+j)%x] + num[j])%MOD;
abc ans = ksm(b);
abc E;
E.jz[0][0] = 1;
ans = jc(E,ans);
printf("%I64d\n",ans.jz[0][k]);
return 0;
}
【38.24%】【codeforces 621E】 Wet Shark and Blocks的更多相关文章
- 【矩阵乘法优化dp】[Codeforces 621E] Wet Shark and Blocks
http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test 2 seconds ...
- JAVA 基础编程练习题24 【程序 24 根据输入求输出】
24 [程序 24 根据输入求输出] 题目:给一个不多于 5 位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. package cskaoyan; public class cskaoya ...
- CODEFORCEs 621E. Wet Shark and Blocks
E. Wet Shark and Blocks time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks dp+矩阵加速
题目链接: http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test2 se ...
- 【38.24%】【POJ 1201】Intervals
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25902 Accepted: 9905 Description You are ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- Codeforces Round #341 (Div. 2) E - Wet Shark and Blocks
题目大意:有m (m<=1e9) 个相同的块,每个块里边有n个数,每个数的范围是1-9,从每个块里边取出来一个数组成一个数,让你求组成的方案中 被x取模后,值为k的方案数.(1<=k< ...
- cf 621E. Wet Shark and Blocks
神奇,矩阵乘法23333333333333333 递推式是很简单的(连我这种不会DP的人都写出来了.) 需要求出的是转移矩阵(还是叫系数矩阵的),也是最这个东西用快速幂. 这个东西的i,j大概就表示从 ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 1.1 Python基础知识 - 变量
1.什么是变量? 变量是可以通过变量名访问的内存地址,变量通常是可变的. 2.怎样去定义? 变量格式: 变量名 = "变量值" 例如: name = "Zhanghk&q ...
- DIV+CSS学习笔记
第十五章 定位 static静态定位(不对它的位置进行改变,在哪里就在那里) 默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明 ...
- Loadrunner--运行场景报Socket descriptor not found错误
今天早上在使用LoadRunner时,报了如下的错误,开始并未看错误以为是录制问题引起,就重新录制了一遍,简单施压看看错误是否还有,结果错误仍然有,如下所示: Error: Socket descri ...
- linux中获取系统时间 gettimeofday函数
linux的man页中对gettimeofday函数的说明中,有这样一个说明: $ man gettimeofday DESCRIPTION The functions gettimeof ...
- RMAN备份到NFS,报错 ORA-27054
使用RMAN备份数据库到NFS挂载到的本地目录/backup 失败,失败提示如下: RMAN-03009: failure of backup command on ORA_DISK_1 channe ...
- google校招在线測试题---2048
先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...
- css3-11 如何改变背景图片的大小和位置
css3-11 如何改变背景图片的大小和位置 一.总结 一句话总结:css3相对css2本身就支持改变背景图片的大小和位置. 1.怎么设置背景不填充padding部分? background-orig ...
- vue项目build后font-awesome不显示问题
解决办法: 修改build目录下的utils.js:添加 publicPath: '../../' // Extract CSS when that option is specified // (w ...
- 在Scope利用Content sharing Widget来分享内容
在最新的Scope Widget中,有一个新的Content Sharing Widget.我们能够利用这个Widget来分享我们的图片到信息.Facebook,Twitter等渠道.比方,在我们的S ...
- [CSS] Conditionally Apply Styles Using Feature Queries @supports
While browsers do a great job of ignoring styles they don’t understand, it can be useful to provide ...