// version 1.1.3 interface Ring { operator fun plus(other: Ring): Ring operator fun times(other: Ring): Ring val value: Int val one: Ring } fun Ring.pow(p: Int): Ring { require(p >= 0) var pp = p var pwr = this.one while (pp-- > 0) pwr *= this return pwr } class ModInt(override val value: Int, val modulo: Int): Ring { override operator fun plus(other: Ring): ModInt { require(other is ModInt && modulo == other.modulo) return ModInt((value + other.value) % modulo, modulo) } override operator fun times(other: Ring): ModInt { require(other is ModInt && modulo == other.modulo) return ModInt((value * other.value) % modulo, modulo) } override val one get() = ModInt(1, modulo) override fun toString() = "ModInt($value, $modulo)" } fun f(x: Ring): Ring = x.pow(100) + x + x.one fun main(args: Array) { val x = ModInt(10, 13) val y = f(x) println("x ^ 100 + x + 1 for x == ModInt(10, 13) is $y") }