forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1427E.go
58 lines (54 loc) · 1.17 KB
/
1427E.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bufio"
. "fmt"
"io"
)
// github.com/EndlessCheng/codeforces-go
func CF1427E(in io.Reader, _w io.Writer) {
out := bufio.NewWriter(_w)
defer out.Flush()
type pair struct {
x, y int64
op byte
}
var x int64
Fscan(in, &x)
ans := []pair{{x, x, '^'}} // write 0
for i := 0; i < 20; i++ {
ans = append(ans, pair{x << i, x << i, '+'}) // write x * 2^k, so we can write any multi of x
}
const mx = 39
basis := [mx + 1]int64{} // xor basis
o:
for i := int64(1); basis[0] == 0; i++ {
bs := []int64{}
for j, y := mx, x*i; j >= 0; j-- {
if y>>j&1 > 0 {
if basis[j] == 0 {
basis[j] = y
y = int64(0)
for k := i; k > 0; k &= k - 1 {
v := k & -k * x
ans = append(ans, pair{y, v, '+'}) // write from 0 to x*i by splitting i to some 2^k
y += v
}
for _, b := range bs {
ans = append(ans, pair{y, b, '^'}) // write from x*i to basis[j]
y ^= b
}
continue o
}
if basis[j] > 0 {
y ^= basis[j]
bs = append(bs, basis[j])
}
}
}
}
Fprintln(out, len(ans))
for _, p := range ans {
Fprintf(out, "%d %c %d\n", p.x, p.op, p.y)
}
}
//func main() { CF1427E(os.Stdin, os.Stdout) }