技巧
dfs 二分
思路
先用dfs将4 7 全排列拿到,再用二分找到第一个>=k的值 累加即可
实现
package main import ( "bufio" . "fmt" "io" "os" "strconv" ) var baseEle [2]string = [2]string{"4", "7"} var allLuckyNumStr []string = make([]string, 0) func dfs(cur int, path string, max int) { if cur == max { allLuckyNumStr = append(allLuckyNumStr, path) return } for i := 0; i < 2; i++ { path += baseEle[i] dfs(cur+1, path, max) path = path[:len(path)-1] } } func NC15291(_r io.Reader, _w io.Writer) { in, out := bufio.NewReader(_r), bufio.NewWriter(_w) defer out.Flush() var l, r int Fscan(in, &l, &r) // 列举出所有幸运数字 for i := 0; i < 9; i++ { dfs(0, "", i + 1) } allLuckyNum := make([]int, len(allLuckyNumStr)+1) for i := 0; i < len(allLuckyNumStr); i++ { allLuckyNum[i], _ = strconv.Atoi(allLuckyNumStr[i]) } allLuckyNum[len(allLuckyNumStr)] = 4444444444 ans := 0 for l <= r { ll, rr := 0, len(allLuckyNumStr) for ll <= rr { mid := (ll + rr) / 2 if l >= allLuckyNum[mid] { ll = mid + 1 } else { rr = mid - 1 } } k := allLuckyNum[ll] min := k if r < min { min = r } ans += k * (min - l + 1) l = 1 + k } Fprintln(out, ans) } func main() { NC15291(os.Stdin, os.Stdout) }