Submission
Status:
PPPPPPPPPPPPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: loop
Problemset: มุมขวาบน
Language: cpp
Time: 0.004 second
Submitted On: 2026-10-10 18:32:47
#include <iostream>
using namespace std;
// After cycle j the spiral ends exactly on the top-right corner, at C(j) = 4j^2 + 2j + 1 (1, 7, 21, 43, ...).
// The next cycle goes down k, left k, up k+1, right k+1 (k = 2j+1); only the final "right" run, while it
// is still left of the old right edge, changes the answer.
int main() {
long long n;
cin >> n;
long long j = 0;
while (4 * (j + 1) * (j + 1) + 2 * (j + 1) + 1 <= n) j++;
long long c = 4 * j * j + 2 * j + 1, k = 2 * j + 1;
if (n <= c + 3 * k) { cout << c << '\n'; return 0; }
long long m = n - (c + 3 * k + 1); // 0 = the new top-left cell, 1.. = cells of the right run
long long missing = 2 * j + 1 - m; // empty cells between it and the old right edge
if (missing >= 2) cout << c << '\n';
else if (missing == 1) cout << "Cannot find top-right corner." << '\n';
else cout << n << '\n';
return 0;
}