This is the exercise I am trying to solve:
Problem statement
Jack, the pirate finally found the treasure.He found that there were infinite numbers of coins but he can collect only N coins and the coins belong to R different countries (coins will be different for each country). Jack want to collect at least one coin of each country. Find the number of ways the coins can be collected. Assume that coins of same countries can't be distinguished and the order of the coins is irrelevant.
Input
T: Number of test case T lines containing N and R N: Number of coins jack can collect R: Number of different countries
Output
For each test case print the number of possibilities of selecting Coins.
Explanation :
In a pile of coins he can only collect 5 coins and there are 3 different coins; Let a, b and c be different coins
(a,a,a,b,c) (a,a,b,b,c) (a,a,b,c,c) (a,b,b,b,c) (a,b,b,c,c) (a,b,c,c,c)
So jack can collect the coins in 6 different ways. Constraint
1<=T<=1000 1<=N<=10 1<=R<=N
I couldn't code a way to find all the solutions mathematically, so I tried a brute-force approach, but I recognize it's a wate of resources, especially when n is large.
test_cases = int(input())
for case in range(test_cases):
solutions = []
n_r = tuple(map(lambda x: int(x), input().split()))
n = n_r[0]
r = n_r[1]
excess = n - r
if r >= n:
print(1)
else:
array = [chr(i) for i in range(97, 97 + r)]
for i in range(10**len(array)):
digits = list(map(lambda x: int(x), str(i)))
if sum(digits) == excess:
solutions.append(i)
print(len(solutions))