480C - Riding in a Lift - CodeForces Solution


combinatorics dp implementation *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
#define x first
#define y second
typedef long long ll;
const int N=5010,mod=1e9+7;
int T,n,a,b,k;  //第i次在j位置的方案数
void add(int a,int b,int c,int diff[]){ //区间[a,b)
    diff[a]=(diff[a]+c)%mod;
    diff[b]=(diff[b]-c+mod)%mod;
}
void solve(){
    cin>>n>>a>>b>>k;
    int diff[n+2];  //差分数组  x到y的范围为(x-d,x+d) 因此可以使用差分数组记录方案数
    memset(diff,0,sizeof diff);
    add(a,min(a+1,n+1),1,diff);  //起点初始化
    for(int i=1;i<=k;i++){
        vector<int>s(n+1,0);
        for(int j=1;j<=n;j++){
            s[j]=(s[j-1]+diff[j])%mod;
        }
        memset(diff,0,sizeof diff);  //初始化 清空上一轮的状态(不清空结果不对)
        for(int j=1;j<=n;j++){
            int d=abs(j-b);
            int l=max(1,j-d+1),r=min(n+1,j+d);
            //cout<<l<<" "<<r<<" "<<j<<" "<<s[j]<<endl;
            add(l,j,s[j],diff);   //不能停在原点 因此分成两部分计算
            add(j+1,r,s[j],diff);
        }
    }
    ll res=0,pre=0;
    for(int i=1;i<=n;i++){  //统计终点在[1,n]的方案数
       pre=(pre+diff[i]+mod)%mod;
       res=(res+pre)%mod;
       //cout<<diff[i]<<" ";
    }
    cout<<res<<endl;
}
int main(){
    T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals