cses

solution to cses exercise problems
git clone git://git.superpozycja.net/cses
Log | Files | Refs | README

commit 5827773e44e07a594bf3c57c31309c2124219ddb
parent d35b65024336b1fed24d3148637951be054a4830
Author: superpozycja <anna@superpozycja.net>
Date:   Wed, 23 Oct 2024 22:01:33 +0200

solve number spiral (really proud of this one though)

Diffstat:
Aintro/number_spiral.cpp | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/intro/number_spiral.cpp b/intro/number_spiral.cpp @@ -0,0 +1,29 @@ +#include <bits/stdc++.h> + +using namespace std; + +void solve() +{ + long y, x, res = 1; + cin >> y >> x; + if (y == x) { + res = x * (x - 1) + 1; + } else if (y > x) { + res = 2*(y/2) * 2*(y/2) + y%2; + int dir = y%2 ? 1 : -1; + res += (x-1) * dir; + } else { + res = (2*((x-1)/2)+1) * (2*((x-1)/2)+1) + (x+1)%2; + int dir = x%2 ? -1 : 1; + res += (y-1) * dir; + } + cout << res << "\n"; +} + +int main() +{ + int t; + cin >> t; + while (t--) + solve(); +}