fork download
  1. using System.Runtime.Versioning;
  2. using static IO;
  3. public class IO
  4. {
  5. public static IO Cin = new();
  6. public static StreamReader reader = new(Console.OpenStandardInput());
  7. public static StreamWriter writer = new(Console.OpenStandardOutput());
  8. public static implicit operator string(IO _) => reader.ReadLine();
  9. public static implicit operator char[](IO _) => reader.ReadLine().ToArray();
  10. public static implicit operator int(IO _) => int.Parse(reader.ReadLine());
  11. public static implicit operator double(IO _) => double.Parse(reader.ReadLine());
  12. public static implicit operator string[](IO _) => reader.ReadLine().Split();
  13. public static implicit operator int[](IO _) => Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
  14. public void Deconstruct(out int a, out int b) { int[] r = Cin; (a, b) = (r[0], r[1]); }
  15. public void Deconstruct(out int a, out int b, out int c) { int[] r = Cin; (a, b, c) = (r[0], r[1], r[2]); }
  16. public static void Loop(int end, Action<int> action, int start = 0, in int add = 1) { for (; start < end; start += add) action(start); }
  17. public static object? Cout { set { writer.Write(value); } }
  18. public static object? Coutln { set { writer.WriteLine(value); } }
  19. public static void Main() { Program.Coding(); writer.Flush(); }
  20. }
  21. class Program
  22. {
  23. static int K { get; set; }
  24. static int[] Pows { get; set; } = null!;
  25. readonly static int[] memo = new int[614_0000];
  26. static int Sk(int n)
  27. {
  28. int ret = 0;
  29. while (n > 0)
  30. {
  31. ret += Pows[n % 10];
  32. n /= 10;
  33. }
  34. return ret;
  35. }
  36. readonly static Stack<int> stack = new();
  37. static int Dp(int n)
  38. {
  39. if (memo[n] > 0) return memo[n];
  40. if (memo[n] < 0)
  41. {
  42. int minimum = n;
  43. for (int ret; (ret = stack.Pop()) != n;) minimum = Math.Min(minimum, ret);
  44. stack.Clear();
  45. return minimum;
  46. }
  47. stack.Push(n);
  48. memo[n] = -614;
  49. return memo[n] = Math.Min(n, Dp(Sk(n)));
  50. }
  51. public static void Coding()
  52. {
  53. (int a, int b, K) = Cin;
  54. Pows = Enumerable.Range(0, 10).Select(x => (int)Math.Pow(x, K)).ToArray();
  55. long result = 0;
  56. for (int i = a; i <= b; i++)
  57. {
  58. result += Dp(i);
  59. }
  60.  
  61. Cout = result;
  62. }
  63.  
  64. }
Success #stdin #stdout 0.13s 40620KB
stdin
100000 400000 6
stdout
5169721292