Skip to content
Snippets Groups Projects
Commit d520b945 authored by guo zling's avatar guo zling
Browse files

重构了mymod.

parent 6709067c
No related branches found
No related tags found
No related merge requests found
......@@ -6,15 +6,15 @@
uint64_t mymod(uint64_t a, uint64_t m){
//先实现基于倍增的取模
uint64_t nowa = a;
for(int i = 63; i; i--){
if(m < (1ull << (64-i))){
if( (m << i) < nowa)
nowa = nowa - (m << i);
}
uint64_t nowm = m;
while (nowm <= a >> 1){
nowm = nowm << 1;
}//m扩大到不超过a的最大。
while (nowm >= m){
if(nowa >= nowm)
nowa = nowa - nowm;
nowm = nowm >> 1;
}
//最后还要判定一次二的零次方
if(nowa > m)
nowa = nowa - m;
return nowa;
}
......@@ -38,7 +38,7 @@ uint64_t multimod(uint64_t a, uint64_t b, uint64_t m) {
uint64_t nowa = mymod(a, m);
uint64_t res = 0;
for(int i = 1; i <= 64; i++){
if(nowa & 1){
if(nowa & 1ull){
res = domod(nowb, res, m);
}
nowb = domod(nowb, nowb, m);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment