多条件稍作修改即可,如:
程序代码:
Function Sol(ByVal sum&, ByVal n&, ParamArray mo()) 'As Long
Dim s(), i&, j&, k&
ReDim s(1 To sum, 1 To n)
For j = 1 To sum
s(j, 1) = 1
For i = 0 To UBound(mo) - 1 Step 2
If j Mod mo(i) = mo(i + 1) Then s(j, 1) = 0: Exit For
Next
Next
For k = 2 To n
For i = 1 To sum - 1
If s(i, k - 1) Then
For j = 1 To sum - i
If s(j, 1) Then s(i + j, k) = s(i + j, k) + s(i, k - 1)
Next
End If
Next
Next
Sol = s(sum, n)
End Function
Sub Calc()
Dim t!
t = Timer
Debug.Print Sol(1000, 9, 3, 0, 5, 0, 5, 2, 7, 1, 7, 5, 11, 3); Timer - t & " seconds"
End Sub
返回:
104857006764168 0.0715625 seconds
即9个未知数的和等于1000,若每个解模3<>0,模5<>0,模5<>2,模7<>1,模7<>5,模11<>3,则有104857006764168 组解,运行0.07秒左右
[此贴子已经被作者于2022-11-25 08:12编辑过]