I've made a solution for this problem:
Task:
Write a program for calculation of the distance to the next space station.
Algorithm:
First base is located at the distance equal to SMK from the beginning of the path (Sirius in our case). The next station is located at the distance of F(SMK) from the first. Third station — at the distance of F(F(SMK)) and so on. Here \$F\$ — is the Top Secret Mars Function (TSMF). Its value is the sum of the cubes of digits its argument in decimal notation (for example \$F(12) = 1^3 + 2^3 > = 9\$). So if the distance from the (\$I − 1\$)-th to \$I\$-th stations is \$X\$, then the distance between \$I\$-th and (\$I + 1\$)-th stations is \$F(X)\$. Your cruiser is located between (\$N − 1\$)-th and \$N\$-th space stations at the distance of \$L\$ from (\$N − 1\$)-th station. Taking \$N\$, \$K\$ (Secret Mars Key) and \$L\$ as input your program should output the distance \$M\$ between your cruiser and \$N\$-th station. Oh, by the way! The value of SMK is always divisible by 3.
Input:
Number \$T\$ (\$2 ≤ T ≤ 33333\$) is placed in the first line of input — it is the number of tests for your program. It followed by the next \$T\$ lines.
Each of these \$T\$ lines contains 3 integer numbers:
\$N\$ (\$2 ≤ N ≤ 33333\$), \$K\$ (\$3 ≤ K ≤ 33333\$) and \$L\$ (\$L ≥ 1\$)
Output:
\$T\$ lines. \$I\$-th line contains the calculated value of \$M\$ for \$I\$-th test case.
It has to run in less than one second and take up less than 16 megabytes of memory.
Sample:
Here is the compiled code:
static void Main()
{
int T = Convert.ToInt32(Console.ReadLine());
for (int k = 0; k < T; k++)
{
string[] split = (Console.ReadLine()).Split(new Char[] { ' ' });
int N = Convert.ToInt32(split[0]);
double K = 0;
string Kst = split[1];
double POST = 0;
for (int i = 0; i < N - 1; i++)
{
for (int j = 0; j <= Kst.Length - 1; j++)
{
int Kindex = int.Parse(Convert.ToString(Kst[j]));
K = K + Kindex*Kindex*Kindex;
}
POST = K;
Kst = Convert.ToString(K);
K = 0;
}
Console.WriteLine(POST - Convert.ToDouble(split[2]));
}
}
But this code doesn't go through the time limit of 1.0 second. How can I improve the speed of this solution?