We are given a sequence: it's formed by concatenating blocks. Block k (starting with k=1) is the integers from 1 to k. So the sequence is:
Block1: [1]
Block2: [1,2]
Block3: [1,2,3]
Block4: [1,2,3,4]
...
Thus the total length after finishing block k is sum_{i=1}^{k} i = k(k+1)/2.
Given n (1-indexed), we need to find which block it falls into, and then the position within that block (i.e., the number from 1 to k). More precisely, find the largest k such that S_k = k(k+1)/2 < n? Actually, if n is exactly S_k, then it's the last element of block k, which is k. But typically we find the smallest k such that S_k >= n. Let k be the block number where the block contains numbers 1..k. Then the starting index of block k is S_{k-1}+1, where S_0 = 0. So if n is between S_{k-1}+1 and S_k inclusive, then the element is (n - S_{k-1}).
Thus we need to compute k from n.
Given n up to 1e14, we need to compute k efficiently. S_k = k(k+1)/2 ~ k^2/2. So k ~ sqrt(2n). Since n up to 1e14, k up to ~ sqrt(2e14) ~ 1.414e7, which is 14 million, which is fine for direct loop? But in C++ we can compute directly with binary search or solve quadratic. Binary search on k in range [1, 2e7] would be fine. But we should be careful: k could be up to about sqrt(2*1e14)=~1.414e7, so 2e7 is safe. Binary search with O(log n) steps is trivial.
Alternatively, solve k = floor((sqrt(8n+1)-1)/2) maybe? Because we need the largest k such that k(k+1)/2 < n, then the block index is k+1 if n > S_k. But due to integer rounding and precision issues with double for n up to 1e14, sqrt(8n+1) up to sqrt(8e14+1) ~ sqrt(8e14)= sqrt(8)*1e7 ~ 2.828e7. Using double (which has 53-bit mantissa, about 15-16 decimal digits) can represent integers up to 2^53 ≈ 9e15 accurately, so n=1e14 is fine, but the square root computation might lose some precision. However, we can compute using integer arithmetic with binary search for safety. Given typical contests, the sqrt method works but I'd use binary search to be precise.
So algorithm:
- Read n (long long).
- Find k such that S_{k-1} < n <= S_k, where S_k = k(k+1)/2.
- Or find the smallest k with S_k >= n.
- Then answer = n - S_{k-1}.
Now we need to compute S_{k-1} = (k-1)k/2.
Edge cases: For n=1, k=1 (since S_1=1 >=1). Then answer = 1 - S_0 =1.
For n=3: S_1=1, S_2=3. Since S_2>=3, k=2. Answer = 3 - S_1 = 3-1=2. Output 2. Works.
For n=5: S_2=3, S_3=6 >=5, so k=3. Answer = 5-3=2. Output 2.
For n=10: S_3=6, S_4=10, so k=4, answer = 10-6=4. Output 4.
For n=55: S_9=910/2=45? Actually S_9=910/2=45, S_10=10*11/2=55, so k=10, answer = 55-45=10. Output 10.
For n=56: S_10=55, S_11=66, so k=11, answer = 56-55=1. Output 1.
Thus correct.
Implementation: use binary search for k.
Define function f(k) = k*(k+1)/2.
Initialize low = 1, high = something like 2e7 or compute high = (long long)sqrt(2n) + 2. For safety, we can set high = 2e7 as that's > sqrt(2e14) ~ 1.414e7. Or compute high = 2sqrt(n)+10. But simpler: while f(high) < n double high. That would be more generic. But n up to 1e14, we can set high=2e7.
Binary search to find smallest k with f(k) >= n.
Then ans = n - f(k-1).
Be careful with overflow: k*(k+1)/2 can exceed 64-bit? For k up to 2e7, k*(k+1) ~ 4e14, divided by 2 ~ 2e14, which fits in 64-bit (max ~9e18). So safe.
Alternatively, we can directly solve: k = ceil((sqrt(8*n+1)-1)/2). But let's use binary search.
Implementation in C++:
#include
using namespace std;
typedef long long ll;
ll sum(ll k) {
return k * (k+1) / 2;
}
int main() {
ll n;
cin >> n;
ll l = 1, r = 2e7; // could be larger, but safe
// ensure r is enough: while sum(r) < n increase r
// Actually compute r appropriately:
r = 1;
while (sum(r) < n) r *= 2;
// then binary search
ll k = -1;
while (l <= r) {
ll mid = (l + r) / 2;
if (sum(mid) >= n) {
k = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
// now k is the block number
ll prev_sum = sum(k-1);
ll ans = n - prev_sum;
cout << ans << endl;
return 0;
}
Test with examples.
But wait: For n = 1, our binary search with r doubling: start r=1, sum(1)=1 >=1, so r=1 remains, then binary search works: l=1,r=1, mid=1, sum>=n => k=1, r=0, exit. ans = 1 - sum(0)=1. Good.
For n=2: r=1, sum(1)=1<2, double r=2, sum(2)=3>=2, binary search: l=1,r=2, mid=1 -> sum=1<2 -> l=2; then l=2,r=2, mid=2 -> sum>=2 -> k=2, r=1 exit? Actually careful. It works. k=2, ans = 2 - sum(1)=2-1=1. But the sequence: position 2 is 1? Actually sequence: 1,1,2,... So position 2 is 1. So ans=1, correct.
But in the problem examples, for n=5 output 2, for n=10 output 4, for n=55 output10, for n=56 output 1. So correct.
Thus solution is straightforward.
But the problem mentions that n up to 1e14, and to use 64-bit.
I will produce the solution.
I will also note that we can do direct formula:
k = floor((sqrt(8*n+1)-1)/2) maybe need to adjust. Let's derive: Let m = floor((sqrt(8n+1)-1)/2). Then S_m = m(m+1)/2 <= n. But if n = S_m exactly, then we are at the end of block m, answer = m. If n > S_m, then block = m+1, answer = n - S_m. So we can compute m directly. However, due to floating error, may get off by one. So better to use binary search or integer arithmetic.
Alternatively, we can solve using integer sqrt: compute x = floor(sqrt(8n+1)). But careful with large numbers.
Given that binary search is easy and safe, I'll go with that.
But the limit is 1e14, and k max is about 1.414e7, binary search takes ~24 iterations, trivial.
I'll also note that we can set high directly as 2e7, but to be safe for exactly up to 1e14 we need to ensure high sum >= n. Let's compute maximum needed: For k = 2e7, sum = 2e7 * (2e7+1)/2 ≈ 2e72e7/2 = 2e14? Actually 2e72e7 = 4e14, half = 2e14, which is > 1e14. So 2e7 is safe. But if n is near 1e14, 2e7 works. So we could just set high = 2e7. However, if n was exactly 1e14, sum(14142135) approx? Let's compute: k ~ sqrt(2n)= sqrt(2e14)= sqrt(2)1e7 ≈ 1.414213562e7. So 2e7 is > that. So safe. But to be more precise, we can compute high = sqrt(2n) + 1000, but that requires floating sqrt. So either use doubling.
I'll just use while loop to double r until sum(r) >= n; that's generic and safe.
Complexities: O(log n) time.
Now produce the final answer.
We need to write solution in some language. The problem expects code probably. I'll output C++.
Potential pitfalls: Use long long, avoid overflow in sum: k*(k+1) might overflow? But k is at most ~2e7, product ~4e14 fits in 64-bit. For safety, we could compute as (k*(k+1LL))/2. It's fine.
Thus solution complete.
Now produce final output: explanation and code.
I'll also mention we can do direct formula but binary search is fine.
Ok, write final solution. ```cpp
#include
using namespace std;
typedef long long ll;
// returns the total number of elements after completing block k: 1+2+...+k = k*(k+1)/2
ll sum(ll k) {
return k * (k + 1) / 2;
}
int main() {
ll n;
cin >> n;
// find the smallest k such that sum(k) >= n using binary search
ll low = 1;
ll high = 1;
while (sum(high) < n) {
high *= 2; // safely expand the upper bound
}
ll k = -1;
while (low <= high) {
ll mid = (low + high) / 2;
if (sum(mid) >= n) {
k = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
// answer = position inside block k
ll ans = n - sum(k - 1);
cout << ans << endl;
return 0;
}