Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Modular-arithmetic/Kotlin/modular-arithmetic.kotlin
Normal file
41
Task/Modular-arithmetic/Kotlin/modular-arithmetic.kotlin
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// version 1.1.3
|
||||
|
||||
interface Ring<T> {
|
||||
operator fun plus(other: Ring<T>): Ring<T>
|
||||
operator fun times(other: Ring<T>): Ring<T>
|
||||
val value: Int
|
||||
val one: Ring<T>
|
||||
}
|
||||
|
||||
fun <T> Ring<T>.pow(p: Int): Ring<T> {
|
||||
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<ModInt> {
|
||||
|
||||
override operator fun plus(other: Ring<ModInt>): ModInt {
|
||||
require(other is ModInt && modulo == other.modulo)
|
||||
return ModInt((value + other.value) % modulo, modulo)
|
||||
}
|
||||
|
||||
override operator fun times(other: Ring<ModInt>): 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 <T> f(x: Ring<T>): Ring<T> = x.pow(100) + x + x.one
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val x = ModInt(10, 13)
|
||||
val y = f(x)
|
||||
println("x ^ 100 + x + 1 for x == ModInt(10, 13) is $y")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue