Private Sub Form_Load() Dim B As Double Dim P As Long Dim M As Long Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, N For i = 1 To N
Input #1, B, P, M ans = B For j = 1 To P - 1 ans = (ans * B) Mod M Next j
花一小時找規則:P
回覆刪除不過B、P在大一點就溢位了
Private Sub Form_Load()
Dim B As Double
Dim P As Long
Dim M As Long
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, N
For i = 1 To N
Input #1, B, P, M
ans = B
For j = 1 To P - 1
ans = (ans * B) Mod M
Next j
Print #2, ans
Next i
Close #2
Close #1
End
End Sub
in.txt
2
1234 3214 1234
2 999 50
out.txt
0
38
這題看似簡單 但是卡在溢位問題
回覆刪除不然就是個輕而一舉的題目了
佑好,
回覆刪除像這樣的題目,考的就是數學。
你的程式似乎是ok的。
但是,(又是但是,哈)
ans = B 改成 ans = B mod m
ans = (ans * B) Mod M 改成
ans = (ans * ( B mod M)) mod M
這樣的話,每次剩下的數字都很小,就不會溢位了。
熊掌好,
回覆刪除小還要更小再拿去算XD
很好懂~謝謝老師
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim B, P, M, Ans
Input #1, ti
For i = 1 To ti
Input #1, B, P, M
'10 2009 9
k = B Mod M
Ans = k
For j = 1 To P - 1
Ans = (Ans * k) Mod M
Next
Print #2, Ans
Next
Close
Close
End
End Sub
arro好,
回覆刪除程式正確。