Пример #1
0
func S(p int64) int64 {
	// p1 = (p-1)! = p-1                                             (mod p)
	// p2 = (p-2)! = 1                                               (mod p)
	// p3 = (p-3)! = inv(p-2)                                        (mod p)
	// p4 = (p-4)! = inv(p-3) * inv(p-2)            =  inv(p-3) * p3 (mod p)
	// p5 = (p-5)! = inv(p-4) * inv(p-3) * inv(p-2) =  inv(p-4) * p4 (mod p)
	var p3 int64 = mathutil.ModularInverse(p-2, p)
	var p4 int64 = (mathutil.ModularInverse(p-3, p) * p3) % p
	var p5 int64 = (mathutil.ModularInverse(p-4, p) * p4) % p
	return (p3 + p4 + p5) % p
}
Пример #2
0
func BuildFactorialTables() {
	factorialMod = make([]int64, maxPrime+1)
	inverseFactorialMod = make([]int64, maxPrime+1)

	var prod int64 = 1
	var invProd int64 = 1
	for i := int64(1); i <= maxPrime; i++ {
		prod = (prod * i) % modPrime
		invProd = (invProd * mathutil.ModularInverse(i, modPrime)) % modPrime
		factorialMod[i] = prod
		inverseFactorialMod[i] = invProd
	}
}