From d520b945e779cd1e2b9378f941fcea8604d3c0d7 Mon Sep 17 00:00:00 2001
From: 221240056-Guo ziliang <221240056@smail.nju.edu.cn>
Date: Fri, 29 Sep 2023 16:17:35 +0800
Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BA=86mymod.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 multimod/multimod.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/multimod/multimod.c b/multimod/multimod.c
index f022d88..c83ef04 100644
--- a/multimod/multimod.c
+++ b/multimod/multimod.c
@@ -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);
-- 
GitLab