ν‹°μŠ€ν† λ¦¬ λ·°

πŸ‘©πŸ»‍πŸ’» 문제

 

11057번: 였λ₯΄λ§‰ 수

였λ₯΄λ§‰ μˆ˜λŠ” 수의 μžλ¦¬κ°€ μ˜€λ¦„μ°¨μˆœμ„ μ΄λ£¨λŠ” 수λ₯Ό λ§ν•œλ‹€. μ΄λ•Œ, μΈμ ‘ν•œ μˆ˜κ°€ 같아도 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μΉœλ‹€. 예λ₯Ό λ“€μ–΄, 2234와 3678, 11119λŠ” 였λ₯΄λ§‰ μˆ˜μ΄μ§€λ§Œ, 2232, 3676, 91111은 였λ₯΄λ§‰ μˆ˜κ°€ μ•„λ‹ˆλ‹€. 수

www.acmicpc.net


✍🏻 ν’€μ΄

🎨 Go

// https://www.acmicpc.net/problem/11057
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)
	fmt.Fprintln(writer, getAscCount(n))
}

func getAscCount(length int) int {
	var count int
	var dp = make([][]int, length+1) // dp[i][j]: i번째 μžλ¦¬μˆ˜μ— jκ°€ 올 λ•Œμ˜ 였λ₯΄λ§‰ 수 경우의 수
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, 10)
	}
	for i := 0; i < 10; i++ {
		dp[1][i] = 1 // 1번째 μžλ¦¬μˆ˜μ— iκ°€ 올 λ•Œμ˜ 였λ₯΄λ§‰ 수 경우의 μˆ˜λŠ” 각각 1이닀
	}
	for i := 1; i < length+1; i++ { // dp[i][j] = dp[i-1][0] + dp[i-1][1] + ... + dp[i-1][j]
		for j := 0; j < 10; j++ {
			for k := 0; k <= j; k++ {
				dp[i][j] += dp[i-1][k]
				dp[i][j] %= 10007
			}
		}
	}
	for i := 0; i < 10; i++ {
		count += dp[length][i]
	}
	return count % 10007
}

🎨 Python3

# https://www.acmicpc.net/problem/11057
import sys

def get_asc_count(n):
    count = 0
    dp = [[0]*10 for _ in range(n+1)]
    for i in range(10):
        dp[1][i] = 1
    for i in range(1, n+1):
        for j in range(10):
            for k in range(j+1):
                dp[i][j] += dp[i-1][k]
                dp[i][j] %= 10007
    for i in range(10):
        count += dp[n][i]
    return count%10007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    print(get_asc_count(n))
728x90
λŒ“κΈ€