【codeforces 821E】Okabe and El Psy Kongroo
2017-10-04 18:44
447 查看
【题目链接】:http://codeforces.com/problemset/problem/821/E
【题意】
一开始位于(0,0)的位置;
然后你每次可以往右上,右,右下3走一步;
(x+1,y+1),(x+1,y),(x+1,y-1)
然后有n条横线,限制你在横线与x轴之间;
问你从(0,0)到(k,0)有多少种行走方案;
k可以很大;
保证横线是连续的,即一条横线完,马上接另外一条横线;
【题解】
如果不考虑那么大的横坐标的话;
用最简单的DP
设f[i][j]表示到(i,j)这个点的方案数;
则有f[i][j] = f[i-1][j+1] + f[i-1][j] + f[i-1][j-1];
在这个DP的基础上,写一个矩阵优化;
构造这么一个矩阵
(1 1 0 ....0) (1 1 1 0 ...0) (0 1 1 1 0 ...0) (0 0 1 1 1 ...0) .... (0 0 0 0 0...1 1) c[i]+1行,c[i]+1列
然后用一个一列的矩阵
f[1] f[2] f[3] ... f[c[i]+1]
去左乘它;
这里f[i]表示当前这一段横线;
到达纵坐标为i的方案有多少种;
把那个矩阵的min(k,b[i])-a[i]次幂求出来;
然后用那个f矩阵去左乘它得到新的f矩阵就好;
每段都这样做;
最后输出f[1];
【Number Of WA】
0
【反思】
没仔细去想矩阵的构造方法,就跳过去了;
还是不敢去做题啊。被吓到了。
【完整代码】
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) #define Open() freopen("F:\\rush.txt","r",stdin) #define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 110; const int G = 16; //矩阵大小 const LL MOD = 1e9 + 7; //模数 struct MX { int v[G+5][G+5]; void O() { ms(v, 0); } void E() { ms(v, 0); for (int i = 1; i <= G; ++i)v[i][i] = 1; } void P() { for (int i = 1; i <= G; ++i) { for (int j = 1; j <= G; ++j)printf("%d ", v[i][j]); puts(""); } } MX operator * (const MX &b) const { MX c; c.O(); for (int k = 1; k <= G; ++k) { for (int i = 1; i <= G; ++i) if (v[i][k]) { for (int j = 1; j <= G; ++j) { c.v[i][j] = (c.v[i][j] + (LL)v[i][k] * b.v[k][j]) % MOD; } } } return c; } MX operator + (const MX &b) const { MX c; c.O(); for (int i = 1; i <= G; ++i) { for (int j = 1; j <= G; ++j) { c.v[i][j] = (v[i][j] + b.v[i][j]) % MOD; } } return c; } MX operator ^ (LL p) const { MX y; y.E(); MX x; memcpy(x.v, v, sizeof(v)); while (p) { if (p&1) y = y*x; x = x*x; p>>=1; } return y; } }xishu; struct abc{ LL a,b; int c; }; int n; LL k; LL f[20],nf[20]; abc a ; int main(){ //Open(); Close(); cin >> n >> k; rep1(i,1,n){ cin >> a[i].a >> a[i].b >> a[i].c; } f[1] = 1; rep1(i,1,n){ xishu.O(); rep1(j,1,a[i].c+1){ int now =j-1; for (int jj = 1;jj <= 3;jj++){ xishu.v[j][now++] = 1; } } xishu = xishu^(min(k,a[i].b)-a[i].a); rep1(j,1,16) nf[j] = 0; rep1(j,1,a[i].c+1){ rep1(k,1,a[i].c+1){ nf[j] = (nf[j]+f[k]*xishu.v[j][k])%MOD; } } rep1(j,1,16) f[j] = nf[j]; } cout << f[1] << endl; return 0; }
相关文章推荐
- Codeforces 821 E. Okabe and El Psy Kongroo
- Okabe and El Psy Kongroo CodeForces - 821E【DP+矩阵快速幂】
- E - Okabe and El Psy Kongroo CodeForces - 821E
- E.Okabe and El Psy Kongroo
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂+dp
- CF round#420 div.2 E Okabe and El Psy Kongroo【矩阵快速幂】
- E.Okabe and El Psy Kongroo
- E.Okabe and El Psy Kongroo
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵快速幂)
- E. Okabe and El Psy Kongroo(递推+矩阵快速幂)
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂
- [刷题]Codeforces Round #420(Div. 2) - E. Okabe and El Psy Kongroo
- 【codefoces821E】 Okabe and El Psy Kongroo【Dp+矩阵快速幂】
- E.Okabe and El Psy Kongroo
- CF821E:Okabe and El Psy Kongroo(dp & 矩阵)
- E.Okabe and El Psy Kongroo
- E.Okabe and El Psy Kongroo
- codeforce#420 E. Okabe and El Psy Kongroo(图论+矩阵快速幂)
- Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo(矩阵快速幂)
- E.Okabe and El Psy Kongroo