如果a,b<m的話a%=m,b%=m;可以拿掉沒關係
code(比較慢,但適用範圍更大,算法取自維基百科:同餘):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ull = unsigned long long; | |
ull _mod_mul(ull a,ull b,ull m){ | |
a%=m,b%=m; | |
ull ans=0; | |
for(;b;b>>=1){ | |
if(b&1){ | |
ans+=a; | |
if(ans>=m)ans-=m; | |
} | |
a<<=1; | |
if(a>=m)a-=m; | |
} | |
if(ans>=m)ans-=m; | |
else if(ans<0)ans+=m; | |
return ans; | |
} |
沒有留言:
張貼留言