题解 | #附加题#

附加题

https://www.nowcoder.com/practice/58b04ed2865f4ff4921290f1bd4ee486

package main

import (
	"fmt"
)

func main() {
	a := 0
	fmt.Scan(&a)
	arr := make([]int, a+1)
	for i := 0; i < a; i++ {
		fmt.Scan(&arr[i])
        arr[i]--
	}
	fmt.Printf("%v", answer(arr))
}

func answer(a []int) int {
    rooms := len(a)
	dp := make([]int, rooms)
	for i := 0; i < rooms-1; i++ {
		dp[i+1] = (dp[i]*2 - dp[a[i]] + 1000000009) % 1000000007
	}
	return dp[rooms-1]
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务