Sub getP(ByVal m As Long, ByVal n As Long, Optional ByRef P As String)
Dim mlen As Long, last As Variant, i As Long, j As Long, temp As Variant
Dim result() As Currency, s() As String, stime As Double
If m < n Then MsgBox "条件错误!": Exit Sub
mlen = 1
stime = Timer
ReDim result(1 To mlen)
result(1) = 1
i = m - n
Do While i < m
i = i + 1
last = 0
For j = 1 To mlen
temp = result(j) * i + last
If temp < 100000 Then
result(j) = temp
last = 0
Else
result(j) = Val(Right(temp, 5))
last = Val(Left(temp, Len(temp) - 5))
End If
Next
Do While Not last = 0
mlen = mlen + 1
ReDim Preserve result(1 To mlen)
If last < 100000 Then
result(mlen) = last
last = 0
Else
result(mlen) = Val(Right(last, 5))
last = Val(Left(last, Len(last) - 5))
End If
Loop
Loop
ReDim s(1 To mlen)
For i = 2 To mlen
s(i) = Format(result(mlen + 1 - i), "00000")
Next
s(1) = result(mlen)
P = Join(s, "")
Debug.Print "P(" & m & "," & n & ")=" & P
Debug.Print "用时 "; FormatNumber(Timer - stime, 8, vbTrue) & " 秒, 结果 " & Len(P) & " 位"
Erase s
Erase result
End Sub
Private Sub Command1_Click()
getP 100000000, 36
End Sub
Sub pailie(ByVal n As Integer)
Dim x() As String
Dim i As Integer, j As Integer, Num As Variant
Dim a() As Integer '记载数组下标
Dim b() As Integer '记载使用标识
ReDim x(1 To n)
For i = 1 To n
x(i) = Right("00" & i, 2)
Next
ReDim a(1 To n)
ReDim b(1 To n)
i = 1
a(1) = 0
Num = 1
Do While i <= n
a(i) = a(i) + 1
If a(i) <= n Then
If b(a(i)) = 0 Then
If i = n Then '达到数组末,则输出组合情况
Debug.Print Num & ":"
For j = 0 To 35
Debug.Print Chr(j \ 12 + 65) & j Mod 12 + 1 & "=" & x(a(j + 1)) & " "; '输出
If j Mod 12 = 11 Then Debug.Print
Next
Debug.Print
Num = Num + 1
i = i - 1
b(a(i)) = 0 '清空使用标识
Else
b(a(i)) = 1 '标记已使用
i = i + 1
a(i) = 0 '重查
End If
End If
Else
i = i - 1
If i = 0 Then Exit Do
b(a(i)) = 0
End If
DoEvents
Loop
End Sub
Private Sub Command1_Click()
pailie 37 'x=37时
End Sub
Sub pailie(ByVal n As Integer)
Dim x() As String
Dim i As Integer, j As Integer, Num As Long
Dim a() As Integer '记载数组下标
Dim b() As Integer '记载使用标识
ReDim x(1 To n)
For i = 1 To n
x(i) = Right("00" & i, 2)
Next
ReDim a(1 To n)
ReDim b(1 To n)
i = 1
a(1) = 0
Num = 1
Do While i <= n
a(i) = a(i) + 1
If a(i) <= n Then
If b(a(i)) = 0 Then
If i = n Then '达到数组末,则输出组合情况
Debug.Print Num & ":"
For j = 0 To 35
Debug.Print Chr(j \ 12 + 65) & j Mod 12 + 1 & "=" & x(a(j + 1)) & " "; '输出
If j Mod 12 = 11 Then Debug.Print
Next
Debug.Print
Num = Num + 1
i = i - 1
b(a(i)) = 0 '清空使用标识
Else
b(a(i)) = 1 '标记已使用
i = i + 1
a(i) = 0 '重查
End If
End If
Else
i = i - 1
If i = 0 Then Exit Do
b(a(i)) = 0
End If
DoEvents
Loop
End Sub
Private Sub Command1_Click()
pailie 37 'x=37时
End Sub