CSES - BAPC 2015 - Results
Submission details
Task:The King's Walk
Sender:KnowYourArchitecture
Submission time:2017-10-17 21:37:23 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.98 sdetails

Code

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

const int mod = 5318008;
int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		int x1, y1, x2, y2;
		cin >> n >> x1 >> y1 >> x2 >> y2;
		if (abs(x1-x2) < abs(y2-y1)) {
			swap(x1, y1);
			swap(x2, y2);
		}
		if (x2 < x1) {
			swap(x1, x2);
			swap(y1, y2);
		}
		x1--;
		y1--;
		x2--;
		y2--;

		vector<int> prev(n);
		vector<int> cur(n);
		int x = x1;
		prev[y1] = 1;
		for (; x < x2; x++) {
			//cur = move(vector<int>(n));
			cur[0] = (prev[0] + prev[1]) % mod;
			cur[n-1] = (prev[n-1] + prev[n-2]) % mod;
			for (int y = 1; y < n-1; y++) {
				cur[y] = (prev[y-1] + prev[y] + prev[y+1]) % mod;
			}
			//for (int i : cur) cout << i << "\t"; cout << endl;
			swap(prev, cur);
		}
		cout << prev[y2] << "\n";
	}
}

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
...