-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDANGER.cs
61 lines (55 loc) · 1.4 KB
/
DANGER.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
// https://www.spoj.com/problems/DANGER/ #formula #game #math
// Finds the last survivor for n people in a circle, where every second person dies.
public static class DANGER
{
// 1
//
// 1 2
// 1
//
// 1 2 3
// 1 3
// 3
//
// 1 2 3 4
// 1 3
// 1
// And so on, like:
// n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17...
// s: 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1 3...
// So the last survivor is 2*(n - 2^(floor(log(n)))) + 1, where the n - term gets
// n's distance past the greatest power of two equal to or less than it.
public static int Solve(string nEncoded)
{
int mantissa = (nEncoded[0] - '0') * 10 + (nEncoded[1] - '0');
int exponent = nEncoded[3] - '0';
int n = mantissa;
for (int e = 0; e < exponent; ++e)
{
n *= 10;
}
return 2 * (n - GreatestPowerOfTwoEqualOrLess(n)) + 1;
}
private static int GreatestPowerOfTwoEqualOrLess(int value)
{
int result = 2;
while (result <= value)
{
result <<= 1;
}
return result >> 1;
}
}
public static class Program
{
private static void Main()
{
string nEncoded;
while (!(nEncoded = Console.ReadLine()).StartsWith("00e0"))
{
Console.WriteLine(
DANGER.Solve(nEncoded));
}
}
}