summaryrefslogtreecommitdiff
path: root/introductory_problems/number_spiral.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-13 12:51:23 -0700
committerJack Sangdahl <[email protected]>2025-02-13 12:51:23 -0700
commit3950be91f9589567d8258c788e10a0dec758e659 (patch)
treeb3aa21a45bf04463c13be47c1501743ee3f6ab67 /introductory_problems/number_spiral.cpp
parent700f2303dfa3307117e8106215c146c419f426cc (diff)
downloadcses-3950be91f9589567d8258c788e10a0dec758e659.tar.gz
cses-3950be91f9589567d8258c788e10a0dec758e659.zip
1071. number spiral
Diffstat (limited to 'introductory_problems/number_spiral.cpp')
-rw-r--r--introductory_problems/number_spiral.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/introductory_problems/number_spiral.cpp b/introductory_problems/number_spiral.cpp
new file mode 100644
index 0000000..95290b7
--- /dev/null
+++ b/introductory_problems/number_spiral.cpp
@@ -0,0 +1,22 @@
+// 1071. Number Spiral
+#include <bits/stdc++.h>
+using namespace std;
+
+long long x, y;
+
+long long solve(long long x, long long y) {
+ long long z = fmax(x, y) - 1;
+ if (z & 1)
+ return x < y ? z * z + x : z * z + 2 * z - y + 2;
+ else
+ return x < y ? z * z + 2 * z - x + 2 : z * z + y;
+}
+
+int main() {
+ static int tmp;
+ scanf("%d", &tmp);
+ for (int i = 0; i < tmp; i++) {
+ scanf("%lld %lld", &x, &y);
+ printf("%lld\n", solve(x, y));
+ }
+}