SDSC 2017 Day 4 T3 Lift

//考场上写的30分可爱小暴力 
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
const int moder = 1000000007;
int n, a, b, k;
int ans;

void dfs(int na, int step){
    if(step == k+1){
        ans %= moder;
        ans++;
        return;
    }
    int delta = abs(na - b) - 1;
    if(delta == 0)  return;
    int l = na - delta;
    int r = na + delta;
    if(l < 1)   l = 1;
    if(r > n)   r = n;
    for(int i = l; i <= r; i++)
        if(i != na) dfs(i, step+1);
}

int main(){
    freopen("lift.in", "r", stdin);
    freopen("lift.out", "w", stdout);
    scanf("%d%d%d%d", &n, &a, &b, &k);
    if(abs(a-b) == 1){
        printf("0\n");
        return 0;
    }
    dfs(a, 1);
    printf("%d\n", ans);
    return 0;
}
//正解DP
//dp[i][j]表示从i层出发还剩j次坐电梯的方案数 
#include<bits/stdc++.h>
using namespace std;
const int moder = 1e9 + 7;
int n, a, b, k;
long long sum[5010], start, end;
int dp[5012][2];

inline int read(){
    int num = 0;
    char c;
    while((c = getchar()) == ' ' || c == '\r' || c == '\n');
    num = c - '0';
    while(isdigit(c = getchar()))   num = num*10 + c - '0';
    return num;
}

int main(){
    n = read(); a = read();
    b = read(); k = read();

    if(abs(a-b) == 1){
        printf("0\n");
        return 0;
    }

    int now = 1, pre = 0;
    for(int i = 1; i <= n; i++)
        dp[i][pre] = 1;

    for(int i = k; i <= 1; i++){
        sum[0] = 0;
        for(int j = 1; j <= n; j++)
            sum[j] = dp[j][pre] + sum[j-1];//预处理前缀和 
        for(int j = 1; j <= n; j++)
            if(j != b){
                int delta = abs(j - b);
                if(j - delta < 0)
                    start=0;
                else
                    start = j - delta;
                start++;
                if(j + delta > 1 + n)
                    end = n + 1;
                else
                    end = j + delta;
                end--;
                dp[j][now] = (sum[end] - sum[start-1] - dp[j][pre]) % moder;
            }
        swap(now, pre);//滚动数组 
    }

    printf("%d\n", dp[a][pre]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值