CSES - DatatÃ¤hti 2022 alku - Results
 Task: Spiraali (Spiral) Sender: mbezirgan Submission time: 2021-10-14 19:48:13 Language: C++17 Status: READY Result: 15
Feedback
groupverdictscore
#1ACCEPTED15
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1details
#20.10 s2details
#30.00 s3details

### Compiler report

```input/code.cpp: In function 'uint64_t GetSpiralValue(int, const Int2&)':
input/code.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^```

### Code

```#include <iostream>
#include <cmath>

struct Int2
{
int x, y;
};

uint64_t GetSpiralValue(int size, const Int2& pos)
{
// Distance form center for each axis
Int2 distance =
{
std::abs(pos.x >= size / 2 ? pos.x - size / 2 : pos.x - size / 2 + 1),
std::abs(pos.y >= size / 2 ? pos.y - size / 2 : pos.y - size / 2 + 1)
};
// Maximum distance from the center tells witch ring it is.
// Maybe can be optimized, but no.
int ringFromCenter = std::max(distance.x, distance.y);
int ringInToCenter = (ringFromCenter <= size / 2 ? size / 2 - ringFromCenter : (size / 2 + 1) - ringFromCenter) - 1;

// Can be optimized as its a linear sum (I think).
uint64_t ringStartPoint = 1 + (uint64_t)size* (uint64_t)size - std::pow(2 * ringFromCenter + 2, 2);

Int2 RelativePos =
{
pos.x - ringInToCenter,
pos.y - ringInToCenter,
};

if (RelativePos.x == 0 && RelativePos.y == 0)
return ringStartPoint;
if (RelativePos.x == 0)
return ringStartPoint + RelativePos.y;
if (RelativePos.y == 2 * ringFromCenter + 1)
return ringStartPoint + RelativePos.x + 2 * ringFromCenter + 1;
if (RelativePos.x == 2 * ringFromCenter + 1)
return ringStartPoint - RelativePos.y + 2*(2 * ringFromCenter + 2) + (2 * ringFromCenter) - 1;
if (RelativePos.y == 0)
return ringStartPoint - RelativePos.x + 2 * (2 * ringFromCenter + 2) + 2 * (2 * ringFromCenter);
}

int main()
{
int n, t;
std::cin >> n;
std::cin >> t;

//Int2* positions = new Int2[t];

//for (int i = 0; i < t; i++)
//{
//	int x, y;
//	std::cin >> x;
//	std::cin >> y;

//	positions[i] = { y - 1, x - 1 };
//}

//for (int i = 0; i < t; i++)
//{
//	auto& pos = positions[i];

//	std::cout << GetSpiralValue(n, pos) << "\n";
//}

//delete[] positions;
for (int y = 0; y < n; y++)
{
for (int x = 0; x < n; x++)
{
std::cout << GetSpiralValue(n, { x, y }) << " ";
}
std::cout << "\n";
}
}```

### Test details

#### Test 1

Group: 1

Verdict: ACCEPTED

input
10 100
1 1
1 2
1 3
1 4
...

correct output
1
36
35
34
33
...

user output
1 36 35 34 33 32 31 30 29 28
2 37 64 63 62 61 60 59 58 27
3 38 65 84 83 82 81 80 57 26
4 39 66 85 96 95 94 79 56 25
5 40 67 86 97 100 93 78 55 24
...

#### Test 2

Group: 2

Verdict:

input
1000 1000
371 263
915 322
946 880
53 738
...

correct output
773533
312166
206053
200080
593922
...

user output
1 3996 3995 3994 3993 3992 399...

#### Test 3

Group: 3

Verdict:

input
1000000000 1000
177757853 827347032
409613589 419171337
739269360 256524697
328695530 896842209
...

correct output
571375684522141210
967321186816598569
762879105851175000
370065046779516790
936897883750373771
...

user output
(empty)