Codeforces 821 E. Okabe and El Psy Kongroo

题意:

有一个人初始在起点(0,0)出发向终点 (K,0) ,每次他可以从(x,y)走到(x + 1, y)(x + 1, y - 1)(x + 1, y + 1)其中一种。现在有N条线段,每条线段都是平行于X轴的。在行进过程中,人是不超过线段高度的。求从起点到终点的方案数。

算法:

有一个非常简单的DP方程

fi,j=fi1,j+fi1,j1+fi1,j+1

考虑优化这个方程,由于题目中保证了线段的高度不超过15,那么想一下 按照套路就直接上矩乘优化。构造一个单位矩阵, ai,i=ai,i+1=ai,i1=1

这里写图片描述

代码:
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;

long long rd() {
    long long x = 0; char c = getchar();
    while (c > '9' || c < '0') c = getchar();
    while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x;
}

const int P = 1e9 + 7;
struct Martix {
    int a[17][17];
    Martix() { memset(a, 0, sizeof a); }
}ans;
long long n, K;
bool fg;

void Add(int &x, int y) {
    x += y;
    if (x >= P) x -= P;
}

Martix mul(Martix A, Martix B, int C) {
    Martix re;
    for (int k = 0; k <= C; k ++) 
        for (int i = 0; i <= C; i ++) 
            for (int j = 0; j <= C; j ++) 
                if (A.a[i][k] && B.a[k][j]) Add(re.a[i][j], 1ll * A.a[i][k] * B.a[k][j] % P);
    return re;
}

void power(long long B, int C) {
    Martix e;
    for (int i = 0; i < 16; i ++) {
        e.a[i][i] = 1;
        if (i) e.a[i][i - 1] = 1;
        if (i + 1 < 16) e.a[i][i + 1] = 1;
    }
    for (int i = C + 1; i < 16; i ++) ans.a[i][0] = 0;
    for (; B; B >>= 1, e = mul(e, e, C)) if (B & 1) ans = mul(ans, e, C);
}

int main() {
    n = rd(), K = rd();
    ans.a[0][0] = 1;
    for (int i = 0; i < n; i ++) {
        long long a = rd() + 1, b = rd(), c = rd();
        if (K < b) b = K, fg = 1;
        power(b - a + 1, c);
        if (fg) break;
    }
    printf("%d\n", ans.a[0][0]);
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值