有多少种方法可以用 2 个固定值填充 n 个位置,并用 2 个选定值之间的值填充其余位置,这样我们就不会重复?
示例:n=4,固定值 1 和 3
case 1 : fix 1 on position 1 and 3 on position 2
1 3 1 1
1 3 1 2
1 3 2 1
1 3 2 2
1 3 1 3
1 3 3 1
1 3 3 3
1 3 2 3
1 3 3 2
case 2 : fix 1 on position 1 and 3 on position 3
1 1 3 1
1 1 3 2
1 2 3 1
1 2 3 2
1 3 3 2
1 2 3 3
1 3 3 1
1 1 3 3
1 3 3 3
now in case 1 and 2 : 1 3 3 3 and 1 3 3 1 and 1 3 3 2 are repeating
case 3 : ....similarly other cases follow
到目前为止我所做的是:nC2 * POWER( (max-min+1) , n-2 ) - duplicates
但无法减去重复项。
最佳答案
我不确定是否理解,但如果我理解得很好,你有 n(数字的数量)和适用的数字范围。 x 是该范围内的数字个数。
您必须将此视为对基数 x 的计数。
例子:
x = 3 (we really don't care about min and max it change nothing)
n = 4
1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 1
1 1 2 2
etc.
所以填充的数量应该是:
POWER(x, n)-POWER(x, n-1)
当 x 是你所说的 max-min+1
对于您的示例,他们有 54 种不同的填充方式
关于algorithm - 用固定值填充 n 个位置的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232873/