コード例 #1
0
        public static BigInt powByModFast(BigInt x, BigInt y, BigInt N)
        {
            if (y.isZero())
            {
                return(new BigInt("1"));
            }
            string binary = Convert.ToString(Int32.Parse(y.toString()), 2);

            binary = new string(binary.ToCharArray().Reverse().ToArray());
            List <BigInt> temp = new List <BigInt>();
            BigInt        last = x;

            temp.Add(last);
            for (int i = 1; i < binary.Length; i++)
            {
                last = mod((multiply(last, last)), N);
                temp.Add(last);
            }
            BigInt res = new BigInt("1");

            for (int i = 0; i < binary.Length; i++)
            {
                if (binary[i] == '1')
                {
                    res = multiply(res, temp[i]);
                }
            }
            res = mod(res, N);
            return(res);
        }
コード例 #2
0
 public static BigInt GCD(BigInt a, BigInt b)
 {
     if (b.isZero())
     {
         return(a);
     }
     else
     {
         return(GCD(b, mod(a, b)));
     }
 }
コード例 #3
0
        public static BigInt powByMod(BigInt x, BigInt y, BigInt N)
        {
            if (y.isZero())
            {
                return(new BigInt("1"));
            }
            BigInt z = BigInt.powByMod(x, divide(y, new BigInt("2")), N);

            if (mod(y, valueOf(2)).isZero())
            {
                return(mod(multiply(z, z), N));
            }
            else
            {
                return(mod(multiply(multiply(z, z), x), N));
            }
        }
コード例 #4
0
        public static BigInt pow(BigInt a, BigInt n)
        {
            if (n.isZero())
            {
                return(BigInt.valueOf(1));
            }

            BigInt c = mod(n, valueOf(2));

            if (equal(valueOf(1), c))
            {
                return(multiply(pow(a, subtract(n, valueOf(1))), a));
            }
            else
            {
                BigInt b = pow(a, divide(n, valueOf(2)));
                return(multiply(b, b));
            }
        }
コード例 #5
0
        public static BigInt divide(BigInt a, BigInt b)
        {
            if (b.isZero())
            {
                throw new ArgumentException("divide to zero");
            }
            int[]  res      = new int[a.number.Length];
            BigInt curValue = new BigInt("0");

            for (int i = a.number.Length - 1; i >= 0; i--)
            {
                curValue.LevelUp();
                curValue.number[0] = a.number[i];
                int x = 0;
                int le = 0, r = BASE;
                while (le <= r)
                {
                    int    m   = (le + r) >> 1;
                    BigInt cur = multiply(b, valueOf(m));
                    if (lessEqualAbs(cur, curValue))
                    {
                        x  = m;
                        le = m + 1;
                    }
                    else
                    {
                        r = m - 1;
                    }
                }
                res[i]   = x;
                curValue = subtract(curValue, multiply(b, valueOf(x)));
            }
            if (less(a, BigInt.ZERO) && !equal(curValue, ZERO))
            {
                return(subtract(new BigInt(deleteLeadingZerosOfArray(res), a.Negative == b.Negative ? false : true), valueOf(1)));
            }
            return(new BigInt(deleteLeadingZerosOfArray(res), a.Negative == b.Negative ? false : true));
        }