[アルゴリズム]第一級-1562:NとM


📑 問題の説明


1からNと書かれたビーズがあります.プログラムを作成し,2 M個の抽出方法の数を出力する.

入力


第1行は、自然数N(3<=N<=10)およびM(2<=M<=N)を与える.

🖥 しゅつりょく


最初の行に結果を出力します.最後の合計を出力します.出力順序はアルファベット順に増加します.

入力例1


4 2

サンプル出力1


1 2
1 3
1 4
2 3
2 4

🖥 プログラムロジック



✅ Answer

import sys
import os

sys.path.insert(0, "/".join(os.getcwd().split("/")[:-2]))
from judge import judge


def DFS(L, s):
    global cnt, result
    if L == m:
        re = []
        for j in range(L):
            re.append(res[j])
        cnt += 1
        result.append(re)
        re = []
    else:
        for i in range(s, n + 1):
            res[L] = i
            DFS(L + 1, i + 1)


@judge()
def solve():
    global m, res, cnt, n, result
    n, m = map(int, input().split())
    res = [0] * m
    cnt = 0
    result = []
    DFS(0, 1)
    return "".join((map(lambda x: " ".join(map(str, x)), result))) + str(cnt)

solve()

解説ありがとうございます