-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMRECAMAN.cs
45 lines (38 loc) · 1.13 KB
/
MRECAMAN.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
// https://www.spoj.com/problems/MRECAMAN/ #ad-hoc #memoization
// Computes a special sequence with an interesting recursive definition.
public static class MRECAMAN
{
private const int _limit = 500000;
private static readonly IReadOnlyList<int> _sequence;
static MRECAMAN()
{
int[] sequence = new int[_limit + 1];
var sequenceSet = new HashSet<int> { 0 };
for (int i = 1; i <= _limit; ++i)
{
int firstOption = sequence[i - 1] - i;
int secondOption = sequence[i - 1] + i;
int chosenOption = firstOption > 0 && !sequenceSet.Contains(firstOption)
? firstOption : secondOption;
sequence[i] = chosenOption;
sequenceSet.Add(chosenOption);
}
_sequence = sequence;
}
public static int Solve(int k)
=> _sequence[k];
}
public static class Program
{
private static void Main()
{
int k;
while ((k = int.Parse(Console.ReadLine())) != -1)
{
Console.WriteLine(
MRECAMAN.Solve(k));
}
}
}