As mentioned some times ago, whenever I am looking for a subject to post, the easiest way is to go to Codeforces.com and simply take a problem from the second division. If I manage to solve it from the first time, I write that it was an interesting problem and if I have to make 3-4 submits, I usually write that it is more than interesting.

This time, we have an interesting problem – I managed to solve it from the first submit, and yet it seemed tough somehow

The idea was to guess the last digit of 1378, put on the power of an input, up to 10^9. As you probably see, this is something really big, thus it would be slow. That’s why I built something like this in excel, just to see how the last digits of 8 change, when we increase the power.

Pretty much, we have 5 cases – when the input is 0, and one for division with remainder 0, 1, 2 and 3. Thus the code was written without debugging and simply submitted.

Here comes the code:

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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class round383_1 { static void Main() { int n = int.Parse(Console.ReadLine()); int result=-1; if (n == 0) { result = 1; }else { switch (n % 4) { case 0: result = 6; break; case 1: result = 8; break; case 2: result = 4; break; case 3: result = 2; break; } } Console.WriteLine(result); } } |

No science fiction, it works! 🙂