Esempio n. 1
0
    private long[,] MultiplyMatrices(long[,] multiplicand, long[,] multiplier)
    {
        long[,] product = new long[2, 2];

        product[0, 0] = _modularArithmetic.Add(_modularArithmetic.Multiply(multiplicand[0, 0], multiplier[0, 0]), _modularArithmetic.Multiply(multiplicand[0, 1], multiplier[1, 0]));
        product[0, 1] = _modularArithmetic.Add(_modularArithmetic.Multiply(multiplicand[0, 0], multiplier[0, 1]), _modularArithmetic.Multiply(multiplicand[0, 1], multiplier[1, 1]));
        product[1, 0] = _modularArithmetic.Add(_modularArithmetic.Multiply(multiplicand[1, 0], multiplier[0, 0]), _modularArithmetic.Multiply(multiplicand[1, 1], multiplier[1, 0]));
        product[1, 1] = _modularArithmetic.Add(_modularArithmetic.Multiply(multiplicand[1, 0], multiplier[0, 1]), _modularArithmetic.Multiply(multiplicand[1, 1], multiplier[1, 1]));

        return(product);
    }
Esempio n. 2
0
    public int solution(string S)
    {
        //This is where result will be stored
        long numberOfZeros = 0;

        //Sanity check
        if (!String.IsNullOrEmpty(S))
        {
            int L = S.Length;

            if (L > 1 && S.StartsWith("0"))
            {
                throw new ArgumentException("Parameter can't contain leading zeros", "S");
            }

            //We will store the value of the N in the modular arithmetic form
            long modularArithmeticNumber = 0;
            //We will also store the number of zeros in a decimal representation of N --> Z
            int numberOfZerosInNumber = 0;

            //We will go most to least significant.
            for (int i = 0; i < L; i++)
            {
                if (_zeroCharacter > S[i] || S[i] > _nineCharacter)
                {
                    throw new ArgumentException("Parameter can contain only digits (0-9)", "S");
                }

                //The current digit --> D
                int digit = _digits[S[i]];

                //numberOfZeros(10 * N + D) = 10 * (numberOfZeros(N) - 1) + N - (9 - D)*Z + 1
                numberOfZeros = _modularArithmetic.Subtract(_modularArithmetic.Add(_modularArithmetic.Multiply(10, numberOfZeros), modularArithmeticNumber), _modularArithmetic.Multiply(numberOfZerosInNumber, 9 - digit));

                if (digit == 0)
                {
                    numberOfZerosInNumber += 1;
                }

                //The value of N at the end of the loop is previous value of N multiplied by 10 and increased by current digit
                modularArithmeticNumber = _modularArithmetic.Add(_modularArithmetic.Multiply(10, modularArithmeticNumber), digit);
            }
        }

        //Return the result
        return((int)_modularArithmetic.Add(numberOfZeros, 1));
    }
Esempio n. 3
0
    //The method to pre-calculate the number of K-sparse integers smaller than 2^I
    private void PrecaluteSparseIntegersCounts(int I, int K)
    {
        //K-sparse numbers smaller than 2^I can be divided into two groups:
        //- numbers smaller than 2^I-1 (there are F[I-1] of them)
        //- numbers smaller than 2^I but not smaller than 2^I-1, their binary representations contain 1 at the position representing 2^I-1 and 0s at the positions representing 2^I-2, 2^I-3, …, 2^I-K-1 (there are F[I-K-1] of them)

        //Above can be described as following recursive equation:
        //F[I] = F[I-1] + F[I-K-1]  for I > 0
        //F[I] = 1                  for I <= 0

        _precalutadeSparseIntegersCounts = new long[I];

        _precalutadeSparseIntegersCounts[0] = 1;

        for (int i = 1; i <= K && i < I; i++)
        {
            _precalutadeSparseIntegersCounts[i] = _precalutadeSparseIntegersCounts[i - 1] + 1;
        }

        for (int i = K + 1; i < I; i++)
        {
            _precalutadeSparseIntegersCounts[i] = _modularArithmetic.Add(_precalutadeSparseIntegersCounts[i - 1], _precalutadeSparseIntegersCounts[i - K - 1]);
        }
    }
Esempio n. 4
0
    private long[,] MultiplyMatrices(long[,] multiplicand, long[,] multiplier)
    {
        int length = multiplicand.GetLength(0);

        long[,] product = new long[length, length];

        for (int i = 0; i < length; i++)
        {
            for (int j = 0; j < length; j++)
            {
                long currentProduct = 0;

                for (int k = 0; k < length; k++)
                {
                    currentProduct = _modularArithmetic.Add(currentProduct, _modularArithmetic.Multiply(multiplicand[i, k], multiplier[k, j]));
                }

                product[i, j] = currentProduct;
            }
        }

        return(product);
    }