diff options
author | Jack Sangdahl <[email protected]> | 2025-02-13 12:51:23 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-13 12:51:23 -0700 |
commit | 3950be91f9589567d8258c788e10a0dec758e659 (patch) | |
tree | b3aa21a45bf04463c13be47c1501743ee3f6ab67 | |
parent | 700f2303dfa3307117e8106215c146c419f426cc (diff) | |
download | cses-3950be91f9589567d8258c788e10a0dec758e659.tar.gz cses-3950be91f9589567d8258c788e10a0dec758e659.zip |
1071. number spiral
-rw-r--r-- | introductory_problems/number_spiral.cpp | 22 | ||||
-rw-r--r-- | introductory_problems/readme.md | 7 |
2 files changed, 29 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)); + } +} diff --git a/introductory_problems/readme.md b/introductory_problems/readme.md index 2c2e44a..80fb417 100644 --- a/introductory_problems/readme.md +++ b/introductory_problems/readme.md @@ -34,3 +34,10 @@ A permutation of integers `1, 2, ..., n` is called beautiful if there are no adj **Input**: The only input line contains an integer `n`. (Example: 5) **Output**: Print a beautiful permutation of integers. If there are several solutions, you may print any of them. If there are no solutions, print "NO SOLUTION". (Example: 4 2 5 3 1) + +### Number Spiral +A number spiral is an infinite grid whose upper-left square has number 1. Your task is to find out the number in row `y` and column `x`. + +**Input**: The first imput line contains an integer `t`: the number of tests. After this, there are `t` lines, each containing integers `y` and `x`. (Example: 3`\n`2 3`\n`1 1`\n`4 2) + +**Output**: For each test, print the number in row `y` and column `x`. (Example: 8 1 15) |