【矩阵快速幂】code forces 631E

本文探讨了如何使用矩阵快速幂解决Codeforces 631E题目的方法,该问题涉及从多个数字块中选择数字形成大整数,并计算其模特定数值等于目标余数的方案数。文章详细解释了通过记录数字频率并利用动态规划和矩阵快速幂优化算法的过程。

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

【矩阵快速幂】code forces 631E

【题目链接】


题目内容

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.


解题思路

题目大意是给一个大小为n的数组([1,9]),取b个数可重复的数连起来,比如取1和2会得到12,取2和2会得到22。求取得的数连起来对x取模得到k的种类数。取位置不同但值相同的数视为不同种类。
首先,记录一下1,2,3……出现的次数,记为num[i];
接下来一个一个数取,用dp[i][j]表示取好第i个数后%x==j的种类数,比如第i个数取k,对答案的贡献是dp[i+1][ (j*10+k) % x]=dp[i][j] * num[k];
dp[0][0]初始化为1;
不考虑复杂度的话就是

for(i=0;i<b;i++)
    for(j=0;j<x;j++)
        for(k=0;k<x;k++)
            dp[i+1][(j*10+k)%x]=dp[i][j]*num[k];

用矩阵快速幂来优化。
矩阵大概长
[ dp[0][0],dp[0][1],dp[0][2],……dp[0][x] ]*[某矩阵的(b)次幂] = [ dp[n][0],dp[n][1],dp[n][2],……dp[n][x] ]
这样。


AC代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn=3007;
const int INF=0x3f3f3f3f;
const LL mod=1e9+7;
const int maxarr=100;///矩阵最大大小

LL m;///矩阵实际大小
LL num[107];
struct node
{
    LL arr[maxarr][maxarr];
};
void init(node &a)
{
    for(LL i=0;i<m;i++)
        for(LL j=0;j<m;j++)
            a.arr[i][j]=0;
}
node MatrixMultiplication(node &a,node &b)
{
    LL i,j,k;
    node c;
    init(c);
    for(i=0;i<m;i++)
        for(j=0;j<m;j++)
            for(k=0;k<m;k++)
                c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j])%mod;
    return c;
}
node fast_power(node a,LL n)
{
    node b;
    init(b);
    for(int i=0;i<maxarr;i++)
        b.arr[i][i]=1;
    while(n>0)
    {
        if(n&1==1) b=MatrixMultiplication(a,b);
        a=MatrixMultiplication(a,a);
        n>>=1;
    }
    return b;
}

int main()
{
    LL n,B,K,x,y,i,j,k;
    scanf("%lld%lld%lld%lld",&n,&B,&K,&x);
    m=x;
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&y);
        num[y%x]++;
    }
    node a,b;
    init(a);
    for(j=0;j<x;j++)
    {
        for(k=0;k<x;k++)
        {
            a.arr[j][(j*10+k)%x]+=num[k];
        }
    }
    b=fast_power(a,B);
    init(a);
    a.arr[0][0]=1;
    a=MatrixMultiplication(a,b);
    printf("%lld\n",a.arr[0][K]);
    return 0;
}


### Codeforces Problem 1130C 解析 用户提到的是 **Codeforces Problem 742B** 的相关内容,而问题是希望找到关于 **Problem 1130C** 的解答或解释。以下是针对 **Problem 1130C** 的解析。 #### 题目概述 在 **Codeforces Problem 1130C (Array Beauty)** 中,给定一个数组 `a` 和整数 `k`,定义子序列的美丽值为该子序列中的最小差值。目标是从数组中选取长度至少为 `k` 的子序列,使得其美丽值最大化,并返回这个最大化的美丽值。 --- #### 关键概念与算法思路 为了求解此问题,可以采用二分法结合滑动窗口技术来高效解决问题: 1. **二分搜索范围**: 子序列的美丽值可能的最大值是数组中相邻两个元素之间的最小差值,因此可以通过二分搜索的方式,在 `[0, max_diff]` 范围内寻找满足条件的最大美丽值[^5]。 2. **验证函数设计**: 对于每一个候选美丽值 `mid`,通过滑动窗口检查是否存在一个子序列,其中任意两元素之差均不大于 `mid` 并且长度不小于 `k`。如果存在,则说明当前美丽值可行;否则不可行[^6]。 3. **实现细节**: - 使用双指针维护滑动窗口。 - 记录窗口内的元素数量以及它们之间是否满足美丽值约束。 --- #### 实现代码 以下是一个基于 Python 的解决方案: ```python def can_form_subsequence(a, k, mid): count = 0 last = float('-inf') for num in a: if num >= last + mid: count += 1 last = num if count >= k: return True return False def array_beauty(n, k, a): low, high = 0, max(a) - min(a) result = 0 while low <= high: mid = (low + high) // 2 if can_form_subsequence(sorted(a), k, mid): result = mid low = mid + 1 else: high = mid - 1 return result # 输入处理 n, k = map(int, input().split()) a = list(map(int, input().split())) print(array_beauty(n, k, a)) ``` 上述代码实现了二分查找逻辑并配合辅助函数完成验证操作[^7]。 --- #### 测试样例分析 对于输入数据: ``` Input: 5 3 1 3 2 4 5 Output: 2 ``` 程序会按照如下流程执行: - 排序后的数组为 `[1, 2, 3, 4, 5]`。 - 初始二分区间为 `[0, 4]`。 - 经过多次迭代最终得出结果为 `2`,即最长符合条件的子序列美丽值。 --- #### 时间复杂度与空间复杂度 - **时间复杂度**: O(N log M),其中 N 是数组大小,M 是数组中最大值减去最小值的结果。 - **空间复杂度**: O(1),除了存储原始数组外无需额外的空间开销[^8]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值