CSES - BAPC 2015 - Results
Submission details
Task:The King's Walk
Sender:Qianyun Guo
Submission time:2017-10-17 21:02:08 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED1.18 sdetails

Code

#include<bits/stdc++.h>
using namespace std;

const int P = 5318008;

int T;
int n, a1, b1, a2, b2;
int dp[5010], pre[5010];

int f(int s, int a, int b) {
    memset(dp, 0, sizeof(dp));
    memset(pre, 0, sizeof(pre));
    dp[a] = 1;
    for (int i = 0; i < s; i++) {
        for (int j = 0; j < n; j++) {
            pre[j] = dp[j];
            if (j > 0) pre[j] += dp[j-1];
            if (j < n-1) pre[j] += dp[j+1];
        }
        for (int j = 0; j < n; j++)
            dp[j] = pre[j] % P;
    }
    return dp[b];
}

int main() {
    cin >> T;
    while (T--) {
        cin >> n >> a1 >> b1 >> a2 >> b2;
        if (abs(a2-a1) > abs(b2-b1))
            cout << f(abs(a2-a1), b2-1, b1-1) << endl;
        else 
            cout << f(abs(b2-b1), a2-1, a1-1) << endl; 
    }
    
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100
315
152 73 251 114
3201
2894 304 697 2576
...

correct output
898608
4727344
240176
248812
1744064
...

user output
898608
4727344
240176
248812
1744064
...