codeforces621E————Wet Shark and Blocks(动态规划,矩阵快速幂)

本文介绍了一种使用矩阵快速幂优化递归算法的方法,解决大规模数据集下的组合计数问题。具体地,面对大量相同数字块组成的序列,如何高效计算由这些数字构成的所有可能整数中,满足特定模条件的组合数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, nbk 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 222662 and 66. None of them gives the remainder 1 modulo 2.

In the third sample integers 1113212331 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个数字的block,你需要从每个block中选出1个组成一个数,问组成的数%x=k的方案数

首先,考虑dp递推方程,dp[i][j]表示考虑i个block,模x等于j的方案数。dp[i][j*10+y]=dp[I][j]*num[y].(其中y可以是从1到9九个数,num[y]表示block中数字y的个数)

然而因为b太大,无法直接递推,所以考虑用矩阵快速幂优化递推关系式。

因为得到的数是模x,所以一共有0到x-1的x种可能性。

用一个矩阵x*1的矩阵存结果,表示这x种答案的方案数,矩阵第一个数为1,其余为0

然后构造系数矩阵,显然系数矩阵第i行第j列代表着通过一个block把答案从j变成i的方案数。所以只要先求出系数矩阵再用矩阵快速幂即可。


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;

const int MAXN = 1000010;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

typedef vector<int> vec;
typedef vector<vec> mat;
mat mat_mul(mat &A, mat &B) {
	mat C(A.size(), vec(B[0].size()));
	for(int i = 0; i < A.size(); i++) {
		for(int j = 0; j < B[0].size(); j++) {
			for(int k = 0; k < B.size(); k++) {
				C[i][j] = ((LL)A[i][k] * B[k][j] + C[i][j]) % mod;
			}
		}
	}
	return C;
}
mat mat_pow(mat A, LL n) {
	mat B(A.size(), vec(A.size()));
	for(int i = 0; i < A.size(); i++) B[i][i] = 1;
	while(n) {
		if(n & 1) B = mat_mul(B, A);
		A = mat_mul(A, A);
		n >>= 1;
	}
	return B;
}

int cnt[10];
int main() {
	int n, b, k, x; //FIN;
	scanf("%d%d%d%d", &n, &b, &k, &x);
	for(int i = 1; i <= n; i++) {
		int t; scanf("%d", &t);
		cnt[t]++;
	}
	mat A(x, vec(x)), B(x, vec(1));
	B[0][0] = 1;
	for(int i = 0; i < x; i++) {
		for(int j = 1; j <= 9; j++) {
			A[(i * 10 + j) % x][i] += cnt[j];
		}
	}
	A = mat_pow(A, b);
	B = mat_mul(A, B);
	printf("%d\n", B[k][0]);
	return 0;
}






### Codeforces 平台上的快速幂算法题目及相关实现 #### 快速幂算法简介 快速幂是一种高效的计算 $a^b \mod c$ 的算法,其时间复杂度为 $O(\log b)$。通过将指数分解成二进制形式并利用平方倍增的方式减少乘法次数来提高效率。 --- #### 题目解析与实现方法 以下是两道来自 Codeforces 平台上涉及快速幂的经典题目及其解决方案: --- ##### **题目一:Codeforces 1594B** 这是一道典型的快速幂应用题,目标是找到第 $k$ 个以 $n$ 为底的幂底数累加的结果[^1]。 ###### 解决方案 给定整数 $n$ 和 $k$,我们需要按照 $k$ 的二进制表示逐位判断哪些位置对应的幂需要被加入最终结果中。具体实现如下: ```cpp #include <iostream> #define ll long long using namespace std; const int MOD = 1e9 + 7; int main() { int t; cin >> t; while (t--) { ll n, k; cin >> n >> k; ll ans = 0, s = 1; // 初始化答案和当前幂值 while (k != 0) { if (k & 1) { // 如果当前位为1,则加上对应幂值 ans = (ans + s) % MOD; } s = s * n % MOD; // 更新幂值 k >>= 1; // 右移一位 } cout << ans << endl; } return 0; } ``` 此代码的核心在于使用 `while` 循环逐步处理 $k$ 的每一位,并根据是否为 $1$ 来决定是否将其对应的幂值加入到总和中。 --- ##### **题目二:CodeForces - 894B Ralph And His Magic Field** 本题同样涉及到快速幂的应用,但更侧重于组合数学中的计数问题[^2]。 ###### 解决方案 对于输入的三个参数 $n$, $m$, 和 $k$,我们可以通过快速幂函数高效地求解 $(2^{(n-1)})^{(m-1)} \mod INF$ 的值。如果 $k=-1$ 且 $(n+m)\%2\neq0$,则直接返回 $0$;否则继续执行快速幂逻辑。 ```cpp #include <iostream> using namespace std; const long long INF = 1e9 + 7; long long fast_pow(long long base, long long exp) { long long result = 1; base %= INF; while (exp > 0) { if (exp & 1) { result = (result * base) % INF; } base = (base * base) % INF; exp >>= 1; } return result; } int main() { long long n, m, k; cin >> n >> m >> k; if (k == -1 && (n + m) % 2 != 0) { cout << "0" << endl; return 0; } cout << fast_pow(fast_pow(2, n - 1), m - 1) << endl; return 0; } ``` 这段程序定义了一个通用的快速幂辅助函数 `fast_pow`,用于简化主流程中的幂运算部分[^2]。 --- #### 总结 以上两个例子展示了如何在不同场景下灵活运用快速幂技术解决问题。无论是简单的累加还是复杂的嵌套幂运算,都可以借助这一技巧显著提升性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值