Problem:
Problem I: Sum of Last Digits

Problem

3つの整数 n, m, k が与えられるので

n1%10  +  n1+m%10  +  n1+2m%10  + ... +  n1+(k-1)m%10

を計算せよ。 a % b は a を b で割ったときの余りを表す。

Input

n m k

1行に n, m, k が与えられる。

Constraints

入力は以下の条件を満たす

0 ≤ n ≤ 1018

0 ≤ m ≤ 109

1 ≤ k ≤ 109

Output

答えを1行に出力せよ。

Sample Input 1

1 1 9

Sample Output 1

9

Sample Input 2

2 1 3

Sample Output 2

14

Sample Input 3

6 11 11

Sample Output 3

66

Sample Input 4

100 7 12

Sample Output 4

0

Sample Input 5

123 123 3

Sample Output 5

11

Solution in Scala (judge verdict: Accepted)

object Main extends App {
  import scala.io.StdIn._
  val Array(n, m, k) = readLine.trim.split(' ').map(_.toLong)
  val base = (n % 10).toInt
  val nm = power(n % 10, m)
  val first = Array.fill(10){-1}
  var whileFirst = -1
  var loopLen = -1
  first(base) = 0
  for (i ← 1 to 11 if whileFirst == -1) {
    if (first(base * power(nm, i) % 10) == -1) {
      first(base * power(nm, i) % 10) = i
    }else {
      whileFirst = first(base * power(nm, i) % 10)
      loopLen = i - whileFirst
    }
  }
  val f = (0 until whileFirst).map(i ⇒ base * power(nm, i) % 10)
  val l = (0 until loopLen).map(i ⇒ base * power(nm, whileFirst + i) % 10)
  val r = (0L until (k - whileFirst) % loopLen).map(i ⇒ base * power(nm, whileFirst + i) % 10)
  println(f.sum + l.sum * ((k - whileFirst) / loopLen) + r.sum)
  def power(base: Long, exp: Long): Int = {
    exp match {
      case 0 ⇒ 1
      case 1 ⇒ (base % 10).toInt
      case _ ⇒ power(base * base % 10, exp / 2) * power(base, exp % 2) % 10
    }
  }
}