func solve(pr *codejam.Problem, d *data) { var unitsDigit, tensDigit, hundredsDigit int if d.num == 0 { unitsDigit, tensDigit, hundredsDigit = 1, 0, 0 } else if d.num == 1 { unitsDigit, tensDigit, hundredsDigit = 5, 0, 0 } else if d.num == 2 { unitsDigit, tensDigit, hundredsDigit = 7, 2, 0 } else { v := (d.num - hundredsOffset) % hundredsPeriod u := hundredsLookup[v+hundredsOffset] - 1 unitsDigit = mod10(u) if u >= 10 { u = (u - unitsDigit) / 10 tensDigit = mod10(u) if u >= 10 { u = (u - tensDigit) / 10 hundredsDigit = mod10(u) } } } pr.Write(fmt.Sprintf("Case #%d: %d%d%d\n", d.testIndex, hundredsDigit, tensDigit, unitsDigit)) }
func solve(pr *codejam.Problem, d *data) { n := len(d.iPos) nf := float64(n) var sums vector for i := 0; i < n; i++ { for j := 0; j < 6; j++ { sums[j] += d.iPos[i][j] / nf } } alphaSq := sums[0]*sums[0] + sums[1]*sums[1] + sums[2]*sums[2] betaSq := sums[3]*sums[3] + sums[4]*sums[4] + sums[5]*sums[5] alphaBeta := sums[0]*sums[3] + sums[1]*sums[4] + sums[2]*sums[5] tmin := -alphaBeta / betaSq var dmin float64 if tmin < 0.0 || betaSq < 0.00000001 { tmin = 0.0 dmin = math.Sqrt(alphaSq) } else { x := sums[0] + sums[3]*tmin y := sums[1] + sums[4]*tmin z := sums[2] + sums[5]*tmin dmin = math.Sqrt(x*x + y*y + z*z) } pr.Write(fmt.Sprintf("Case #%d: %.8f %.8f\n", d.testIndex, dmin, tmin)) }
func solve(pr *codejam.Problem, testCaseIndex int, patternStr string, root *trie, wordSize int) { pattern := string2Pattern(patternStr, wordSize) nw := root.count(pattern) pr.Write(fmt.Sprintf("Case #%d: %d\n", testCaseIndex, nw)) }
func solve(pr *codejam.Problem, vd *vectorData) { sort.Ints(vd.xs) sort.Ints(vd.ys) minSum := vd.scalar() pr.Write(fmt.Sprintf("Case #%d: %d\n", vd.testIndex, minSum)) }
func solve(pr *codejam.Problem, d *data) { memo := make(map[[4]int]int) v := [4]int{0, d.nc + 1, 0, len(d.released)} ng := coins(d.released, v, memo) pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ng)) }
func solve(pr *codejam.Problem, d *data) { root := buildTree(d) if symmetric(d, root, 2) { pr.Write(fmt.Sprintf("Case #%d: SYMMETRIC\n", d.testIndex)) } else { pr.Write(fmt.Sprintf("Case #%d: NOT SYMMETRIC\n", d.testIndex)) } }
func solve(pr *codejam.Problem, d *data) { var lookup [36]int for i := 0; i < 36; i++ { lookup[i] = -1 } n := len(d.val) if n == 1 { pr.Write(fmt.Sprintf("Case #%d: 1\n", d.testIndex)) return } countUniq := 0 for i := 0; i < n; i++ { c := d.val[i] j := index(c) if lookup[j] == -1 { if countUniq == 0 { lookup[j] = 1 } else if countUniq == 1 { lookup[j] = 0 } else { lookup[j] = countUniq } countUniq++ } } p := 1 r := 0 var base int if countUniq == 1 { base = 2 } else { base = countUniq } for i := n - 1; i >= 0; i-- { c := d.val[i] j := index(c) if lookup[j] == -1 { panic("digit not set") } r += p * lookup[j] p *= base } pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, r)) }
func solve(pr *codejam.Problem, d *data) { n := len(d.pairs) ais := make([]int, n) bis := make([]int, n) for i := 0; i < n; i++ { ais[i] = i bis[i] = i } psa := &pairSorter{ indices: ais, pairs: d.pairs, column: 0, } psb := &pairSorter{ indices: bis, pairs: d.pairs, column: 1, } sort.Sort(psa) sort.Sort(psb) var nw int for i := 0; i < n-1; i++ { v := d.pairs[ais[i]][1] k := sort.Search(n, func(j int) bool { return d.pairs[bis[j]][1] >= v }) // count common indices from ais[i+1:] and bis[:k] var ba bitset.BitSet var bb bitset.BitSet for _, c := range ais[i+1:] { ba.Set(uint(c)) } for _, c := range bis[:k] { bb.Set(uint(c)) } nw += int(ba.IntersectionCardinality(&bb)) } pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, nw)) }
func solve(pr *codejam.Problem, d *data) { tscsc, ok := powerSort(2, d.xs) if !ok { pr.Write(fmt.Sprintf("Case #%d: 0\n", d.testIndex)) } else { ns := 0 for _, sc := range tscsc { ns += swapWays(sc) } pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ns)) } }
func solve(pr *codejam.Problem, d *data) { var nw int root := &node{} for _, path := range d.es { root.declarePath(path) } for _, path := range d.ns { nw += root.declarePath(path) } pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, nw)) }
func solve(pr *codejam.Problem, d *data) { b := make(batch, d.flavors) for _, c := range d.customers { if len(c.malted) == 1 && len(c.unmalted) == 0 { v := c.malted[0] b[v] = 1 d.malted(b, v) } } found := d.search(b) if found { pr.Write(fmt.Sprintf("Case #%d: %s\n", d.testIndex, b)) } else { pr.Write(fmt.Sprintf("Case #%d: IMPOSSIBLE\n", d.testIndex)) } }
func solve(pr *codejam.Problem, d *data) { // bigger than any number of swaps minZero := 4*len(d.m) + 1 ns := minZero for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { n, ok := solveF(d.m, byte(i), byte(j)) if ok && n < ns { ns = n } } } if ns == minZero { pr.Write(fmt.Sprintf("Case #%d: IMPOSSIBLE\n", d.testIndex)) } else { pr.Write(fmt.Sprintf("Case #%d: %d\n", d.testIndex, ns)) } }
func solve(pr *codejam.Problem, d *data) { alphas := make([]float64, len(d.points)) for k, _ := range d.points { alphas[k] = d.search(k) } var buf bytes.Buffer first := true for _, v := range alphas { if first { first = false } else { buf.WriteString(" ") } buf.WriteString(strconv.FormatFloat(v*100.0, 'f', 9, 64)) } pr.Write(fmt.Sprintf("Case #%d: %s\n", d.testIndex, buf.String())) }