← Home
For problem statement at 0-999/700-799/700-709/703/problemE.txt this is a correct solution, but verifier at 0-999/700-799/700-709/703/verifierE.go ends with All tests passed can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type State struct {
	length int
	sum    int64
}

const INF_LEN = 10000
const INF_SUM = int64(2e18)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	if !scanner.Scan() {
		return
	}
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	k, _ := strconv.ParseInt(scanner.Text(), 10, 64)

	a := make([]int64, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	if k == 1 {
		min_val := int64(2e18)
		min_idx := -1
		for i, val := range a {
			if val < min_val {
				min_val = val
				min_idx = i
			}
		}
		fmt.Println(1)
		fmt.Println(min_idx + 1)
		return
	}

	temp := k
	var primes []int64
	var max_exponents []int
	D := 1

	for i := int64(2); i*i <= temp; i++ {
		if temp%i == 0 {
			count := 0
			for temp%i == 0 {
				count++
				temp /= i
			}
			primes = append(primes, i)
			max_exponents = append(max_exponents, count)
			D *= (count + 1)
		}
	}
	if temp > 1 {
		primes = append(primes, temp)
		max_exponents = append(max_exponents, 1)
		D *= 2
	}

	r := len(primes)
	multipliers := make([]int, r)
	if r > 0 {
		multipliers[r-1] = 1
		for i := r - 2; i >= 0; i-- {
			multipliers[i] = multipliers[i+1] * (max_exponents[i+1] + 1)
		}
	}

	tuples := make([][]int, D)
	for u := 0; u < D; u++ {
		tuples[u] = make([]int, r)
		temp_u := u
		for i := 0; i < r; i++ {
			tuples[u][i] = temp_u / multipliers[i]
			temp_u %= multipliers[i]
		}
	}

	dp := make([]State, D)
	for i := range dp {
		dp[i] = State{INF_LEN, INF_SUM}
	}
	dp[0] = State{0, 0}

	history := make([][]int16, n+1)
	for i := range history {
		history[i] = make([]int16, D)
		for j := range history[i] {
			history[i][j] = -1
		}
	}

	next_dp := make([]State, D)
	Y := make([]int, r)

	for i := 1; i <= n; i++ {
		a_i := a[i-1]
		temp_a := a_i
		useful := false
		for j := 0; j < r; j++ {
			count := 0
			for temp_a%primes[j] == 0 {
				count++
				temp_a /= primes[j]
			}
			if count > max_exponents[j] {
				count = max_exponents[j]
			}
			Y[j] = count
			if count > 0 {
				useful = true
			}
		}

		if !useful {
			continue
		}

		copy(next_dp, dp)

		for u := 0; u < D; u++ {
			if dp[u].length == INF_LEN {
				continue
			}
			v := 0
			for j := 0; j < r; j++ {
				sum_exp := tuples[u][j] + Y[j]
				if sum_exp > max_exponents[j] {
					sum_exp = max_exponents[j]
				}
				v += sum_exp * multipliers[j]
			}

			cand_len := dp[u].length + 1
			cand_sum := dp[u].sum + a_i

			if cand_len < next_dp[v].length || (cand_len == next_dp[v].length && cand_sum < next_dp[v].sum) {
				next_dp[v] = State{cand_len, cand_sum}
				history[i][v] = int16(u)
			}
		}

		dp, next_dp = next_dp, dp
	}

	if dp[D-1].length >= INF_LEN {
		fmt.Println("-1")
		return
	}

	var result []int
	curr_u := D - 1
	for i := n; i >= 1; i-- {
		prev := history[i][curr_u]
		if prev != -1 {
			result = append(result, i)
			curr_u = int(prev)
		}
	}

	fmt.Println(len(result))
	for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
		result[i], result[j] = result[j], result[i]
	}
	for i, idx := range result {
		if i > 0 {
			fmt.Print(" ")
		}
		fmt.Print(idx)
	}
	fmt.Println()
}