Question address: http://codeforces.com/contest/479/problem/E
The dynamic transfer equation of this question is very interesting, but the most obvious one is k*n*n The complexity is obviously not possible. So I thought of using line segment trees to maintain DP information, but only k*n is already on the verge of TLE. . Adding a log will cause TLE, so it won't work. After searching on the Internet, I found that a little trick of using prefix sum can be used. = =! I have used it many times and never thought of it. . . . Poor IQ. . So the rest is simple.
I originally thought that it would be impossible to walk to the other side of b, so I just counted one side. . But I didn’t realize it until I wrote it. . Counting only one side is not as short as counting both sides. . So just forget it.
The code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const LL mod=1e9+7;LL dp[6000], sum[3][6000];int main(){ LL n, a, b, k, i, j; while(scanf("%I64d%I64d%I64d%I64d",&n,&a,&b,&k)!=EOF) { memset(dp,0,sizeof(dp)); memset(sum,0,sizeof(sum)); dp[a]=1; for(i=a;i<=n;i++) sum[0][i]=1; for(i=1;i<=k;i++) { sum[i&1][0]=0; for(j=1;j<=n;j++) { if(j<b) { dp[j]=(sum[i+1&1][(j+b-1)/2]+mod-dp[j])%mod; } else if(j>b) { dp[j]=(sum[i+1&1][n]+mod-sum[i+1&1][(j+b)/2]+mod-dp[j])%mod; } sum[i&1][j]=(sum[i&1][j-1]+dp[j])%mod; //printf("%I64d ",dp[j]); } //puts(""); } printf("%I64d\n",sum[k&1][n]); } return 0;}