private Mpair divideAndMod(List <char> lhs, List <char> rhs) { if (less(lhs, rhs)) { Mpair mpair = new Mpair("0".ToList(), lhs); return(mpair); } Mpair tmp = divideAndMod(lhs, Add(rhs, rhs)); tmp = new Mpair(Add(tmp.Key, tmp.Key), tmp.Value); if (less(tmp.Value, rhs)) { return(tmp); } tmp = new Mpair(Add(tmp.Key, "1".ToList()), Subtract(tmp.Value, rhs)); return(tmp); }
public Mpair longDivision(List <char> number, int divisor) { List <char> module = new List <char>(); if (dic[number[number.Count - 1]] % divisor == 0) { module.Add('0'); } else { module.Add('1'); } if (number.Count == 1) { Mpair corner = new Mpair(); int rnum = (dic[number[0]] / 2); corner.Key.Add(rdic[rnum]); corner.Value = module; return(corner); } List <char> ans = new List <char>(); int idx = 0; int temp = (int)(number[idx] - '0'); while (temp < divisor) { temp = temp * 10 + (int)(number[idx + 1] - '0'); idx++; } ++idx; while (number.Count > idx) { ans.Add((char)(temp / divisor + '0')); temp = (temp % divisor) * 10 + (int)(number[idx] - '0'); idx++; } ans.Add((char)(temp / divisor + '0')); if (ans.Count == 0) { ans.Add('0'); } Mpair ret = new Mpair(ans, module); return(ret); }
public List <char> modPower(List <char> value, List <char> power, List <char> mod) { if (power[0] == '0' && power.Count == 1) { return("1".ToList()); } value = module(value, mod); Mpair ans = longDivision(power, 2); List <char> res = modPower(value, ans.Key, mod); res = module(res, mod); res = new List <char>(Logmultiply(res, res)); res = module(res, mod); if (ans.Value.Count == 1 && ans.Value[0] == '1') { res = new List <char>(Logmultiply(res, value)); } res = module(res, mod); return(res); }