GT考试 (dp+矩阵乘法)

GT考试

[Link]([题目详情 - HNOI2008]GT考试 - HydroOJ)

题意

阿申准备报名参加 G T GT GT 考试,准考证号为 n n n 位数 X 1 X 2 ⋯ X n X1X2⋯Xn X1X2Xn,他不希望准考证号上出现不吉利的数字。

他的不吉利数字 A 1 A 2 ⋯ A m A1A2⋯Am A1A2Am m m m 位,不出现是指 X 1 X 2 ⋯ X n X1X2⋯Xn X1X2Xn 中没有恰好一段等于 A 1 A 2 ⋯ A m A1A2⋯Am A1A2Am A 1 A1 A1 和$ X1$ 可以为 0 0 0

请输出不出现不吉利的数的号码有多少种?

题解

要统计方案数,考虑 d p dp dp维护。

我们设 f [ i , j ] : 表 示 长 度 位 i 且 后 缀 与 不 吉 利 数 前 缀 最 长 匹 配 长 度 j 的 方 案 数 f[i,j]:表示长度位i且后缀与不吉利数前缀最长匹配长度j的方案数 f[i,j]:ij

状态转移时我们考虑当前状态能转移到哪些状态去,也就是第 i + 1 i+1 i+1位填 0 ∼ 9 0\sim9 09对下一个状态的影响。

状态转移方程: f [ i + 1 ] [ k ] = f [ i ] [ 0 ] ∗ a [ 0 ] [ k ] + f [ i ] [ 1 ] ∗ a [ 1 ] [ k ] + . . . + f [ i ] [ m − 1 ] ∗ a [ m − 1 ] [ k ] f[i+1][k]=f[i][0]*a[0][k]+f[i][1]*a[1][k]+...+f[i][m-1]*a[m-1][k] f[i+1][k]=f[i][0]a[0][k]+f[i][1]a[1][k]+...+f[i][m1]a[m1][k]

因为不吉利数是已知的,因此后缀与前缀匹配的数字是什么也是已知的,一次每一种转移对应的系数 a [ j ] [ k ] a[j][k] a[j][k]也是可以求出来的,是个固定值,因此 f [ i ] 和 f [ i + 1 ] f[i]和f[i + 1] f[i]f[i+1]的关系是线性的,因此可以矩阵乘法来加速,即 f [ i + 1 ] = f [ i ] ∗ A f[i+1]=f[i]*A f[i+1]=f[i]A

转移矩阵 A A A中的元素 a [ j ] [ k ] a[j][k] a[j][k]表示原来与不吉利数匹配长度由 j j j k k k的方案数。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 25, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N][N];
char str[N];
void mul(int c[][N], int a[][N], int b[][N]) {
    int tmp[N][N] = {0};
    for (int i = 0; i < m; i ++ )
        for (int j = 0; j < m; j ++ )
            for (int t = 0; t < m; t ++ )
                tmp[i][j] = (tmp[i][j] + a[i][t] * b[t][j]) % k;
    memcpy(c, tmp, sizeof tmp);
}
int qmi(int b) {
    int f[N][N] = {1};
    while (b) {
        if (b & 1) mul(f, f, a);
        b >>= 1;
        mul(a, a, a);
    }
    int res = 0;
    for (int i = 0; i < m; i ++ ) res = (res + f[0][i]) % k;
    return res;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m >> k;
    cin >> str + 1;
    for (int i = 2, j = 0; i <= m; i ++ ) {
        while (j && str[j + 1] != str[i]) j = ne[j];
        if (str[j + 1] == str[i]) j ++;
        ne[i] = j;
    }
    for (int j = 0; j < m; j ++ )
        for (char c = '0'; c <= '9'; c ++ ) {
            int k = j;
            while (k && str[k + 1] != c) k = ne[k];
            if (str[k + 1] == c) k ++;
            if (k < m) a[j][k] ++;
        }
    cout << qmi(n) << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
矩阵乘法优化动态规划是用来解决矩阵连乘积的最优计算次序的问题。这个问题可以通过动态规划的方法来解决。首先,我们定义一个二维数组dp[i][j],其中dp[i][j]表示计算矩阵Ai到Aj的最优计算次序所需的最少乘法次数。然后,我们可以使用递推的方式来计算dp[i][j]的值。 具体的递推步骤如下: 1. 初始化dp[i][i]为0,表示只有一个矩阵时,不需要进行乘法操作。 2. 对于dp[i][j],我们需要枚举一个分割点k,将矩阵连乘积分成两部分,即Ai到Ak和Ak+1到Aj。我们可以通过遍历所有可能的分割点k,来求解dp[i][j]的最小值。 3. 对于每个分割点k,我们可以使用递归的方式求解dp[i][k]和dp[k+1][j]。 4. 根据动态规划的思想,我们可以使用一个循环来遍历所有可能的分割点k,并更新dp[i][j]的值。 最终,当我们计算完所有的dp[i][j]后,dp[n]就表示了矩阵A1到An的最优计算次序所需的最少乘法次数。 这个方法的时间复杂度为O(n^3),其中n表示矩阵的个数。通过使用动态规划来优化矩阵连乘积的计算次序,可以大大减少计算量,提高算法的效率。引用<span class="em"&gt;1</span&gt;<span class="em"&gt;2</span&gt;<span class="em"&gt;3</span&gt; #### 引用[.reference_title] - *1* *3* [矩阵连乘积问题——动态规划](https://blog.csdn.net/qq_43633063/article/details/105943437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [动态规划 矩阵连乘优化](https://blog.csdn.net/u012785169/article/details/100677011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值