CF 479E Riding in a Lift prefix and DP
Input n a B k has n floors. The starting point of n floors cannot be reached at Layer a and layer B. Assuming that the current position on layer x can reach layer y at each time. | x-y | <| x-B | Number of different solutions for k requests
Dp [I] [j] is the number of solutions that reach layer j for the I-th dp [I] [j] = sum (dp [I-1] [k]) | k-j | <| k-B |
K that meets the condition is a continuous segment with prefix and Optimization
#include
#include
#include
using namespace std;typedef long long LL;const int maxn = 5010;const int mod = 1000000007;LL dp[maxn][maxn];int main(){int n, a, b, m;while(scanf("%d %d %d %d", &n, &a, &b, &m) != EOF){memset(dp, 0, sizeof(dp));for(int i = a; i <= n; i++)dp[0][i] = 1;for(int i = 1; i <= m; i++){for(int j = 1; j <= n; j++){if(j == b){dp[i][j] += dp[i][j-1];continue;}if(j < b){int d = b-j;int x = j+(b-j-1)/2;dp[i][j] += dp[i-1][x]-(dp[i-1][j]-dp[i-1][j-1]);dp[i][j] %= mod;}else{int d = j-b;int x = j-(j-b-1)/2;dp[i][j] += dp[i-1][n]-dp[i-1][x-1]-(dp[i-1][j]-dp[i-1][j-1]);}dp[i][j] += dp[i][j-1];dp[i][j] %= mod;}}//for(int j = 1; j <= n; j++)printf("%I64d\n", (dp[m][n]+mod)%mod);}return 0;}