Created
March 1, 2019 09:58
-
-
Save sunary/cefd9d1692745159d89750d50052992e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// same https://play.golang.org/p/3vGkbJ8OiNC | |
package main | |
const C = 12345678 | |
func power(x, y int64) int64 { | |
a := int64(1) | |
for ; ; { | |
if (y & 1) != 0 { | |
a = x * a | |
} | |
y = y >> 1 | |
if y == 0 { | |
return a | |
} | |
x *= x | |
} | |
return a | |
} | |
func calm(a, n int64) int64 { | |
if a > C { | |
a %= C | |
} | |
if n == 0 { | |
return 1 | |
} | |
if n == 1 || a == 0 || a == 1 { | |
return a | |
} | |
loopTime := int64(1) | |
b := a | |
for ; ; { | |
if b > C { | |
break | |
} | |
b *= a | |
loopTime += 1 | |
} | |
loopPower := n / loopTime | |
if loopPower == 0 { | |
return power(a, n-loopTime*loopPower) | |
} | |
return power(a, n-loopTime*loopPower) * calm(power(a, loopTime), loopPower) | |
} | |
func calc(n int64) int64 { | |
s := int64(0) | |
for i := int64(1); i <= n; i++ { | |
s = (s + calm(i, i)) % C | |
} | |
return s | |
} | |
func main() { | |
println(calc(5000000)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment