← Home
For problem statement at 1000-1999/1000-1099/1020-1029/1029/problemF.txt this is a correct solution, but verifier at 1000-1999/1000-1099/1020-1029/1029/verifierF.go ends with All tests passed! can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func getDivisors(n uint64) []uint64 {
	var small, large []uint64
	for i := uint64(1); i*i <= n; i++ {
		if n%i == 0 {
			small = append(small, i)
			j := n / i
			if j != i {
				large = append(large, j)
			}
		}
	}
	// append reversed large to small to get ascending order
	for i := len(large) - 1; i >= 0; i-- {
		small = append(small, large[i])
	}
	return small
}

func fits(divs []uint64, n, W, H uint64) bool {
	if len(divs) == 0 {
		return false
	}
	idx := sort.Search(len(divs), func(i int) bool { return divs[i] > W })
	if idx == 0 {
		return false
	}
	d := divs[idx-1]
	return n/d <= H
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var a, b uint64
	if _, err := fmt.Fscan(in, &a, &b); err != nil {
		return
	}
	s := a + b
	adivs := getDivisors(a)
	bdivs := getDivisors(b)

	var best uint64 = ^uint64(0)

	canFit := func(W, H uint64) bool {
		if fits(adivs, a, W, H) || fits(adivs, a, H, W) {
			return true
		}
		if fits(bdivs, b, W, H) || fits(bdivs, b, H, W) {
			return true
		}
		return false
	}

	for i := uint64(1); i*i <= s; i++ {
		if s%i == 0 {
			W := i
			H := s / i
			if canFit(W, H) {
				p := 2 * (W + H)
				if p < best {
					best = p
				}
			}
		}
	}

	fmt.Println(best)
}