您的位置:首页 > 其它

Codeforces 821 E. Okabe and El Psy Kongroo

2017-06-26 22:00 519 查看
题意:

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

算法:

有一个非常简单的DP方程fi,j=fi−
4000
1,j+fi−1,j−1+fi−1,j+1

考虑优化这个方程,由于题目中保证了线段的高度不超过15,那么想一下按照套路就直接上矩乘优化。构造一个单位矩阵,ai,i=ai,i+1=ai,i−1=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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: