- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 167 lines of Go from the credited upstream file 359C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "time"8)9 1011type node59 struct {12 lr [2]*node5913 priority uint14 key int6415 value int16 subCnt int17}18 19func (o *node59) size() int {20 if o != nil {21 return o.subCnt22 }23 return 024}25 26func (o *node59) maintain() { o.subCnt = 1 + o.lr[0].size() + o.lr[1].size() }27 28func (o *node59) rotate(d int) *node59 {29 x := o.lr[d^1]30 o.lr[d^1] = x.lr[d]31 x.lr[d] = o32 o.maintain()33 x.maintain()34 return x35}36 37type treap59 struct {38 rd uint39 root *node5940}41 42func (t *treap59) fastRand() uint {43 t.rd ^= t.rd << 1344 t.rd ^= t.rd >> 1745 t.rd ^= t.rd << 546 return t.rd47}48 49func (t *treap59) size() int { return t.root.size() }50 51func (t *treap59) _put(o *node59, key int64, value int) *node59 {52 if o == nil {53 return &node59{priority: t.fastRand(), key: key, value: value, subCnt: 1}54 }55 if d := o.cmp(key); d >= 0 {56 o.lr[d] = t._put(o.lr[d], key, value)57 if o.lr[d].priority > o.priority {58 o = o.rotate(d ^ 1)59 }60 } else {61 o.value += value62 }63 o.maintain()64 return o65}66 67func (t *treap59) put(key int64, value int) { t.root = t._put(t.root, key, value) }68 69func (t *treap59) _delete(o *node59, key int64) *node59 {70 if o == nil {71 return nil72 }73 if d := o.cmp(key); d >= 0 {74 o.lr[d] = t._delete(o.lr[d], key)75 } else {76 if o.lr[1] == nil {77 return o.lr[0]78 }79 if o.lr[0] == nil {80 return o.lr[1]81 }82 d = 083 if o.lr[0].priority > o.lr[1].priority {84 d = 185 }86 o = o.rotate(d)87 o.lr[d] = t._delete(o.lr[d], key)88 }89 o.maintain()90 return o91}92 93func (t *treap59) delete(key int64) { t.root = t._delete(t.root, key) }94 95func (o *node59) cmp(a int64) int {96 b := o.key97 if a == b {98 return -199 }100 if a < b {101 return 0102 }103 return 1104}105 106func (t *treap59) min() (min *node59) {107 for o := t.root; o != nil; o = o.lr[0] {108 min = o109 }110 return111}112 113func CF359C(_r io.Reader, out io.Writer) {114 const mod = 1_000_000_007115 pow := func(x, n int64) (res int64) {116 x %= mod117 res = 1118 for ; n > 0; n /= 2 {119 if n%2 > 0 {120 res = res * x % mod121 }122 x = x * x % mod123 }124 return125 }126 min := func(a, b int64) int64 {127 if a > b {128 return b129 }130 return a131 }132 in := bufio.NewReader(_r)133 var n, x int134 s := int64(0)135 Fscan(in, &n, &x)136 a := make([]int64, n)137 for i := range a {138 Fscan(in, &a[i])139 s += a[i]140 }141 142 t := &treap59{rd: uint(time.Now().UnixNano())/2 + 1}143 for _, v := range a {144 t.put(s-v, 1)145 }146 147 ans := int64(1)148 for t.size() > 0 {149 top := t.min()150 k, e := top.value, top.key151 if k%x > 0 {152 ans = pow(int64(x), min(s, e))153 break154 }155 e2 := int64(0)156 for k%x == 0 {157 k /= x158 e2++159 }160 t.delete(e)161 t.put(e+e2, k)162 }163 Fprint(out, ans)164}165 166167