main.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // The exponential of 11
  2. // 3fsv3 - collabedit.com
  3. package main
  4. import "fmt"
  5. // Node ...
  6. type Node struct {
  7. val int
  8. next *Node
  9. }
  10. func reverse(head *Node) *Node {
  11. if head == nil || head.next == nil {
  12. return head
  13. }
  14. dummy := Node{-1, nil}
  15. tail := head.next
  16. for tail != nil {
  17. tmp := tail.next
  18. tail.next = dummy.next
  19. dummy.next = tail
  20. tail = tmp
  21. }
  22. head.next = dummy.next
  23. return head
  24. }
  25. func pow(n int) string {
  26. base := []byte{'1', '1'}
  27. res := []byte{'1'}
  28. for n != 0 {
  29. if n&1 == 1 {
  30. res = mul(res, base)
  31. }
  32. base = mul(base, base)
  33. n >>= 1
  34. }
  35. for l, r := 0, len(res)-1; l < r; l, r = l+1, r-1 {
  36. res[l], res[r] = res[r], res[l]
  37. }
  38. return string(res)
  39. }
  40. func mul(a, b []byte) []byte {
  41. m, n := len(a), len(b)
  42. c := make([]byte, m+n)
  43. for i := range c {
  44. c[i] = '0'
  45. }
  46. for i := 0; i < m; i++ {
  47. for j := 0; j < n; j++ {
  48. res := (a[i] - '0') * (b[j] - '0')
  49. c[i+j] += res
  50. }
  51. }
  52. mod := 0
  53. for i := 0; i < len(c); i++ {
  54. c[i] += byte(mod)
  55. mod = int(c[i]-'0') / 10
  56. c[i] = byte(int(c[i]-'0')%10 + '0')
  57. }
  58. i := m + n
  59. for ; 1 <= i; i-- {
  60. if c[i-1] != '0' {
  61. break
  62. }
  63. }
  64. if i == 0 {
  65. return c[:i+1]
  66. }
  67. return c[:i]
  68. }
  69. func main() {
  70. fmt.Println(pow(9999))
  71. }