import scala.annotation.tailrec import scala.io.StdIn object Problem278 { // 素因数分解 def primeFactorization(n: Long): List[Long] = { @tailrec def factor(n: Long, div: Long, result: List[Long]): List[Long] = { if (n < div * div) { n :: result } else if (n % div == 0) { factor(n / div, div, div :: result) } else { factor(n, div + 1, result) } } factor(n, 2, Nil) } // 等比数列の1項を計算 def f(x: (Long, Int)): BigInt = (BigInt(x._1).pow(x._2 + 1) - 1) / (x._1 - 1) def proc(n: Long): BigInt = { if (n <= 2) return 1 // 4(even): 4n+2 -> div 2 // 5(odd) : 5n -> div 5 val v = if (n % 2 == 0) n / 2 else n val pf = primeFactorization(v) val group = pf.groupBy(x => x).map(x => (x._1, x._2.size)) // 等比数列の和 group.map(f).product } def main(args: Array[String]) { val n = StdIn.readLong() val result = proc(n) println(result) } }