Codeforces 621E Wet Shark and Blocks 【矩阵加速dp】
2016-02-01 14:04
375 查看
E. Wet Shark and Blocks
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
Sample test(s)
input
output
input
output
input
output
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个且相同。你必须在每组选一个数,组成一个新数sum,使得sum % x == k,问方案数 % (1e9+7)。
思路:考虑dp。设置dp[i][j]表示前i个数所组成的数 % x == j的方案数。
转移有dp[i][(j*10+t)%x] = dp[i-1][j]。后面可以写成求和形式。转移相同,用矩阵加速。
AC代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.
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
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个且相同。你必须在每组选一个数,组成一个新数sum,使得sum % x == k,问方案数 % (1e9+7)。
思路:考虑dp。设置dp[i][j]表示前i个数所组成的数 % x == j的方案数。
转移有dp[i][(j*10+t)%x] = dp[i-1][j]。后面可以写成求和形式。转移相同,用矩阵加速。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (100000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
void add(LL &x, LL y)
{
x += y;
x %= MOD;
}
int n, b, k, X;
struct Matrix{
LL a[110][110];
};
Matrix multi(Matrix x, Matrix y)
{
Matrix z; CLR(z.a, 0LL);
for(int i = 0; i < X; i++)
{
for(int k = 0; k < X; k++)
{
if(x.a[i][k] == 0) continue;
for(int j = 0; j < X; j++)
add(z.a[i][j], x.a[i][k] * y.a[k][j]);
}
}
return z;
}
Matrix Pow(Matrix x, int n)
{
Matrix y; CLR(y.a, 0LL);
for(int i = 0; i < X; i++) y.a[i][i] = 1LL;
while(n)
{
if(n & 1)
y = multi(x, y);
x = multi(x, x);
n >>= 1;
}
return y;
}
int cnt[10];
int main()
{
Ri(n); Ri(b); Ri(k); Ri(X);
Matrix ori, res; CLR(ori.a, 0LL); CLR(cnt, 0);
W(n)
{
int t; Ri(t);
cnt[t]++;
}
for(int i = 0; i < X; i++)
for(int j = 0; j <= 9; j++)
add(ori.a[i][(i*10+j)%X], 1LL*cnt[j]);
res = Pow(ori, b); Pl(res.a[0][k]);
return 0;
}
相关文章推荐
- Codeforces 621C Wet Shark and Flowers 【期望】
- jquery的淡入淡出效果
- 南京优步上线黄金区域,接单可享更高奖励!
- JAVA 发送下载文件
- alpha版、beta版、rc版的意思
- Sql日期时间格式转换
- Codeforces 621B Wet Shark and Bishops 【dp】
- Redis学习手册(Key操作命令)
- Codeforces 621A Wet Shark and Odd and Even 【水题】
- Mysql中count(*),DISTINCT的使用方法和效率研究
- E:Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied) E:Unable to lock
- poj1258 Agri-Net(最小生成树)
- ASP.NET Core 1.0 Configuration 配置管理
- js中的逻辑与(&&)和逻辑或(||)
- 299. Bulls and Cows
- 启动Tomcat错误:he JRE_HOME environment variable is not
- swift-结构体
- Java返回距离当前时间段
- Take good care of your eyes
- Android-StrictMode-性能优化