Esempio n. 1
0
    private static void Main()
    {
        //// input below

        string[] size = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int rows = int.Parse(size[0]);
        int cols = int.Parse(size[1]);

        // set the grid
        matrix = new BigInteger[rows, cols];

        string[] foodCoordinatesXY = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int foodX = int.Parse(foodCoordinatesXY[0]);
        int foodY = int.Parse(foodCoordinatesXY[1]);

        // set dog position
        matrix[InitialDogePosX, InitialDogePosY] = 1;

        int numOfEnemies = int.Parse(Console.ReadLine());

        // set the enemies
        for (int i = 0; i < numOfEnemies; i++)
        {
            string[] enemyXY = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int enemyX = int.Parse(enemyXY[0]);
            int enemyY = int.Parse(enemyXY[1]);

            matrix[enemyX, enemyY] = -1;
        }

        BigInteger possibleWays = CountPossibleWays(foodX, foodY);
        Console.WriteLine(possibleWays);
    }
Esempio n. 2
0
        static void Main(string[] args)
        {
            RSA    rsa  = new RSA();
            string text = "Smelova Valeria Vladimirovna";

            Console.WriteLine($"Text: {text}");
            var stopwatch = Stopwatch.StartNew();

            BigInteger[] encryptedTextRSA = rsa.Encrypt(text);
            stopwatch.Stop();
            Console.WriteLine($"Encrypted text: {encryptedTextRSA.Select(el => el.ToString()).Aggregate((prev, current) => prev + " " + current)}");
            Console.WriteLine($"Encryption time: {stopwatch.ElapsedTicks} ticks");
            stopwatch.Restart();
            Console.WriteLine($"Decrypted text: {rsa.Decrypt(encryptedTextRSA)}");
            stopwatch.Stop();
            Console.WriteLine($"Decryption time: {stopwatch.ElapsedTicks} ticks");

            Console.WriteLine();

            ElGamal elGamal = new ElGamal();

            Console.WriteLine($"Text: {text}");
            stopwatch.Restart();
            BigInteger[,] encryptedTextElGamal = elGamal.Encrypt(text);
            stopwatch.Stop();
            Console.WriteLine($"Encrypted text: {string.Join(" ", encryptedTextElGamal.Cast<BigInteger>())}");
            stopwatch.Restart();
            Console.WriteLine($"Encryption time: {stopwatch.ElapsedTicks} ticks");
            Console.WriteLine($"Decrypted text: {elGamal.Decrypt(encryptedTextElGamal)}");
            stopwatch.Stop();
            Console.WriteLine($"Decryption time: {stopwatch.ElapsedTicks} ticks");
            Console.ReadKey();
        }
Esempio n. 3
0
        static void MatrixFiller(BigInteger[,] matrix)
        {
            int a = matrix.GetLength(0);
            int b = matrix.GetLength(1);

            matrix[a - 1, 0] = 1;
            BigInteger buildHelper = 2;

            for (int row = a - 2; row >= 0; row--)
            {
                for (int rowHelper = row, col = 0; rowHelper < a && col < b; rowHelper++, col++)
                {
                    matrix[rowHelper, col] = buildHelper;
                }
                buildHelper = buildHelper * 2;
            }
            for (int col = 1; col < b; col++)
            {
                for (int colHelper = col, row = 0; colHelper < b && row < a; colHelper++, row++)
                {
                    matrix[row, colHelper] = buildHelper;
                }
                buildHelper = buildHelper * 2;
            }

            //for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
            //{
            //    for (int col = 0; col < matrix.GetLength(1); col++)
            //    {
            //        int r = matrix.GetLength(0) - 1 - row;
            //        matrix[row, col] = (BigInteger)Math.Pow(2, r + col);
            //    }
            //}
        }
        private static void FillMatrix()
        {
            matrix = new BigInteger[rDimension, cDimension];

            BigInteger pow = 1;
            for (int i = rDimension - 1; i >= 0; i--)
            {
                matrix[i, 0] = pow;
                pow *= 2;
            }

            pow = 1;
            for (int i = 0; i < cDimension; i++)
            {
                matrix[rDimension - 1, i] = pow;
                pow *= 2;
            }

            for (int i = 1; i < cDimension; i++)
            {
                pow = matrix[rDimension - 1, i];
                for (int j = rDimension - 1 - 1; j >= 0; j--)
                {
                    pow *= 2;
                    matrix[j, i] = pow;
                }
            }
        }
Esempio n. 5
0
        public void GenerateTable(int power, List<int> polynom)
        {
            this.power = power;
            int size = (int)Math.Pow(2, power);
            Ltable = new BigInteger[size, size];
            BigInteger[,] ltableTemp = new BigInteger[size, size];

            CtableGenerator cGenerator = new CtableGenerator();
            TtableGenerator tGenerator = new TtableGenerator();
            cGenerator.GenerateTable(size);
            tGenerator.GenerateTable(power, polynom);

            BigInteger[,] tranparentCMatirix = TransporateMatrix(cGenerator.Ctable, size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    ltableTemp[i, j] = MatrixCellMultResult(size, tranparentCMatirix, tGenerator.Ttable, i, j);
                }
            }

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Ltable[i, j] = MatrixCellMultResult(size, ltableTemp, tranparentCMatirix, i, j);
                }
            }
        }
        public static BigInteger Fib(BigInteger n)
        {
            if (n < 0)
            {
                throw new ArgumentException("The fibo value cannot be negative");
            }
            if (n <= 1)
            {
                return(n);
            }

            BigInteger[,] result = { { 1, 0 }, { 0, 1 } };
            BigInteger[,] fiboM  = { { 1, 1 }, { 1, 0 } };

            while (n > 0)
            {
                if (n % 2 == 1)
                {
                    Mult2x2Matrix(result, fiboM);
                }
                n /= 2;
                Mult2x2Matrix(fiboM, fiboM);
            }

            return(result[1, 0]);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var rowsCount = int.Parse(Console.ReadLine());
            var colsCount = int.Parse(Console.ReadLine());

            var movesCount = int.Parse(Console.ReadLine());

            var moves = Console.ReadLine()
                        .Split(' ')
                        .Select(int.Parse)
                        .ToArray();

            BigInteger[,] matrix = FillTheMatrix(rowsCount, colsCount);
            bool[,] helpMatrix   = new bool[rowsCount, colsCount];
            CycleMatrix(moves, helpMatrix);
            BigInteger sum = 0;

            for (int r = 0; r < matrix.GetLength(0); r++)
            {
                for (int c = 0; c < matrix.GetLength(1); c++)
                {
                    if (helpMatrix[r, c])
                    {
                        sum += matrix[r, c];
                    }
                }
            }
            Console.WriteLine(sum);
        }
Esempio n. 8
0
        static BigInteger FindAllPossibleWays(BigInteger[,] matrix, int foodRow, int foodCol)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if ((row == 0 && col == 0) || (matrix [row, col] == -1))
                    {
                        matrix[row, col] += 1;
                    }
                    else if (row > 0 && col > 0)
                    {
                        matrix[row, col] = matrix[row - 1, col] + matrix[row, col - 1];
                    }
                    else if (row == 0 && col > 0)
                    {
                        matrix[row, col] = matrix[row, col - 1];
                    }
                    else if (row > 0 && col == 0)
                    {
                        matrix[row, col] = matrix[row - 1, col];
                    }

                    if (row == foodRow && col == foodCol)
                    {
                        return(matrix[row, col]);
                    }
                }
            }

            return(0);
        }
Esempio n. 9
0
        private static void Movements(BigInteger[,] matrix, int pawnCurrentX, int pawnCurrentY, int[] positionsAsNums, int r, int c, int n)
        {
            int[]      positionRowArray = new int[n];
            int[]      positionColArray = new int[n];
            BigInteger sum = 0;

            for (int i = 0; i < positionsAsNums.Length; i++)
            {
                int currentNum         = positionsAsNums[i];
                int coeff              = Math.Max(r, c);
                int currentPositionRow = currentNum / coeff;
                int currentPositionCol = currentNum % coeff;
                positionRowArray[i] = currentPositionRow;
                positionColArray[i] = currentPositionCol;
            }
            for (int numberMoves = 0; numberMoves < n; numberMoves++)
            {
                int positionRow = positionRowArray[numberMoves];
                int positionCol = positionColArray[numberMoves];
                sum         += SumToNextMovement(pawnCurrentX, pawnCurrentY, positionRow, positionCol, matrix);
                pawnCurrentX = positionRow;
                pawnCurrentY = positionCol;
            }
            sum = sum + 1;
            Console.WriteLine(sum);
        }
    public static BigInteger fib(int n)
    {
        if (n == 0)
        {
            return(BigInteger.Zero);
        }
        if (n == 1 || n == 2 || n == -1)
        {
            return(BigInteger.One);
        }
        if (n == -2)
        {
            return(-1 * BigInteger.One);
        }

        int cnt = Math.Abs(n);

        BigInteger[,] m1 = { { BigInteger.One, BigInteger.One }, { BigInteger.One, BigInteger.Zero } };
        BigInteger[,] m2 = (BigInteger[, ])m1.Clone();

        while (cnt > 0)
        {
            if (cnt % 2 == 1)
            {
                m2 = Matrix2x2Multiply(m2, m1);
            }

            m1   = Matrix2x2Multiply(m1, m1);
            cnt /= 2;
        }


        return((n < 0 && n % 2 == 0) ? m2[1, 1] * -1 : m2[1, 1]);
    }
        public static BigInteger fib(BigInteger n)
        {
            BigInteger[,] result = { { BigInteger.One, BigInteger.One }, { BigInteger.One, BigInteger.Zero } };

            bool negated = false;

            switch (n.CompareTo(BigInteger.Zero))
            {
            case -1:
                n       = BigInteger.Negate(n);
                negated = true;
                break;

            case 0:
                return(BigInteger.Zero);
            }

            power(result, n - BigInteger.One);

            if (negated)
            {
                return(BigInteger.Equals(n % BigInteger.Parse("2"), BigInteger.One) ? result[0, 0] : BigInteger.Negate(result[0, 0]));
            }
            return(result[0, 0]);
        }
        //method for filling the matrix
        static void FillTheMatrix(BigInteger[,] matrix)
        {
            var curFill = (BigInteger)1;

            for (int curCol = 0;
                 curCol < matrix.GetLength(1);
                 curCol++)
            {
                matrix[r - 1, curCol] = curFill;
                curFill <<= 1;
            }

            for (int curCol = 0;
                 curCol < c;
                 curCol++)
            {
                curFill   = matrix[r - 1, curCol];
                curFill <<= 1;

                for (int curRow = r - 2;
                     curRow >= 0;
                     curRow--)
                {
                    matrix[curRow, curCol] = curFill;
                    curFill <<= 1;
                }
            }
        }
Esempio n. 13
0
        static void Main(string[] args)
        {
            ReadInput();

            coef   = Math.Max(rows, cols);
            matrix = new BigInteger[rows, cols];
            FillMatrix();

            int currRow = rows - 1;
            int currCol = 0;

            for (int i = 0; i < movesCount; i++)
            {
                int code      = codes[i];
                int targetCol = code % coef;
                int targetRow = code / coef;

                MakeHorizontalMove(currRow, currCol, targetCol);
                currCol = targetCol;

                MakeVerticalMove(currRow, currCol, targetRow);
                currRow = targetRow;
            }

            Console.WriteLine(sum);
        }
Esempio n. 14
0
        private static BigInteger[,] MakeMatrix(BigInteger[,] matrix, int r, int c)
        {
            BigInteger[,] result = new BigInteger[r, c];

            int currentRowPower = 0;

            int        currentPower  = currentRowPower;
            BigInteger currentNumber = Power(2, currentRowPower);

            int leftIndex = 0;

            do
            {
                for (int i = r - 1; i >= 0; i--)
                {
                    result[i, leftIndex] = currentNumber;
                    currentPower++;
                    currentNumber = Power(2, currentPower);
                }
                leftIndex++;
                currentRowPower++;
                currentPower  = currentRowPower;
                currentNumber = Power(2, currentPower);
            } while (leftIndex < c);

            return(result);
        }
Esempio n. 15
0
        private static void FillMatrix(BigInteger[,] matrix)
        {
            BigInteger initialValue = 2;
            BigInteger sum          = 0;

            for (int col = 1; col < matrix.GetLength(1); col++)
            {
                for (int row = 0; row <= col; row++)
                {
                    matrix[row, col - row] = initialValue;
                }
                initialValue = initialValue * 2;
            }

            for (int row = 1; row < matrix.GetLength(0); row++)
            {
                int tempRow = row;
                for (int col = matrix.GetLength(1) - 1; col > 0; col--)
                {
                    matrix[tempRow, col] = initialValue;
                    tempRow++;
                    if (tempRow >= matrix.GetLength(0))
                    {
                        break;
                    }
                }
                initialValue = initialValue * 2;
            }
        }
Esempio n. 16
0
        public BigIntMatrix GenerateRandomInvertibleMatrix(BigInteger p, double l0, bool isVectorMatrix)
        {
            BigIntMatrix result     = new BigIntMatrix(rowsCount, colsCount);
            bool         inversible = false;
            BigIntMatrix matInv     = new BigIntMatrix(this.rowsCount, this.colsCount);

            do
            {
                if (isVectorMatrix)
                {
                    result.GenerateRandomVector(p, l0);
                }
                else
                {
                    result.GenerateRandomMatrix(p, l0);
                }
                try
                {
                    matInv     = result.GaussJordanModInverse(p);
                    inversible = true;
                }
                catch (Exception ex)
                {
                    inversible = false;
                }
            } while (!inversible);

            Mat = result.Mat;
            return(matInv);
        }
Esempio n. 17
0
        static void Main(string[] args)
        {
            int rows = int.Parse(Console.ReadLine());
            int cols = int.Parse(Console.ReadLine());

            currentRow = rows - 1;
            matrix     = new BigInteger[rows, cols];
            FillMatrix(rows, cols, matrix);
            //PrintMatrix(matrix, rows, cols);
            int numberOfMoves = int.Parse(Console.ReadLine());

            int[] moves = Console.ReadLine()
                          .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                          .Select(int.Parse)
                          .ToArray();
            int coeff = Math.Max(rows, cols);

            for (int i = 0; i < numberOfMoves; i++)
            {
                int targetRow = moves[i] / coeff;
                int targetCol = moves[i] % coeff;
                MoveToPosition(targetRow, targetCol);
            }
            Console.WriteLine(result);
        }
Esempio n. 18
0
        static void Main(string[] args)
        {
            int[] filedSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int   rows      = filedSize[0];
            int   cols      = filedSize[1];

            field = new int[rows, cols];
            paths = new BigInteger[rows, cols];

            int[] targetLocation = Console.ReadLine().Split().Select(int.Parse).ToArray();
            tagetRow  = targetLocation[0];
            targetCol = targetLocation[1];

            int enemiesCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < enemiesCount; i++)
            {
                int[] enemyLocation = Console.ReadLine().Split().Select(int.Parse).ToArray();
                int   enemyRow      = enemyLocation[0];
                int   enemyCol      = enemyLocation[1];
                field[enemyRow, enemyCol] = EnemyCell;
            }

            BigInteger pathsCount = FindPaths();

            Console.WriteLine(pathsCount);
        }
Esempio n. 19
0
    private static void Main()
    {
        //// input below

        string[] size = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int      rows = int.Parse(size[0]);
        int      cols = int.Parse(size[1]);

        // set the grid
        matrix = new BigInteger[rows, cols];

        string[] foodCoordinatesXY = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int      foodX             = int.Parse(foodCoordinatesXY[0]);
        int      foodY             = int.Parse(foodCoordinatesXY[1]);

        // set dog position
        matrix[InitialDogePosX, InitialDogePosY] = 1;

        int numOfEnemies = int.Parse(Console.ReadLine());

        // set the enemies
        for (int i = 0; i < numOfEnemies; i++)
        {
            string[] enemyXY = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int      enemyX  = int.Parse(enemyXY[0]);
            int      enemyY  = int.Parse(enemyXY[1]);

            matrix[enemyX, enemyY] = -1;
        }

        BigInteger possibleWays = CountPossibleWays(foodX, foodY);

        Console.WriteLine(possibleWays);
    }
Esempio n. 20
0
        private static void Input()
        {
            // On the first line there will be the numbers N and M, separated by a single space.
            var line = Console.ReadLine().Split(' ');
            n = int.Parse(line[0]);
            m = int.Parse(line[1]);
            field = new BigInteger[n, m];

            // On the second line there will be the integer numbers Fx and Fy, separated by a single space.
            line = Console.ReadLine().Split(' ');
            fx = int.Parse(line[0]);
            fy = int.Parse(line[1]);

            // On the third line there will be the number K – the number of Doge`s enemies. Many enemies.
            var k = int.Parse(Console.ReadLine());

            for (int i = 0; i < k; i++)
            {
                // On the next K lines there will be the X and Y coordinates for each Doge`s enemy, separated by a space.
                line = Console.ReadLine().Split(' ');
                var enemyX = int.Parse(line[0]);
                var enemyY = int.Parse(line[1]);

                field[enemyX, enemyY] = Enemy;
            }
        }
Esempio n. 21
0
        static void FillTheMatrix(BigInteger[,] toFill)
        {
            var curFill = (BigInteger)1;

            // Fill Last Row
            for (int curCol = 0;
                 curCol < toFill.GetLength(1);
                 curCol++)
            {
                toFill[sizeRows - 1, curCol] = curFill;
                curFill <<= 1;
            }

            for (int curCol = 0;
                 curCol < sizeCols;
                 curCol++)
            {
                curFill   = toFill[sizeRows - 1, curCol];
                curFill <<= 1;

                for (int curRow = sizeRows - 2;
                     curRow >= 0;
                     curRow--)
                {
                    toFill[curRow, curCol] = curFill;
                    curFill <<= 1;
                }
            }
        }
Esempio n. 22
0
        public Multiplicator(ITransformer transformer)
        {
            var key     = transformer.Transform(new byte[transformer.BlockSize]);
            var initial = new BigInteger(key, true, true);

            this.table = BuildTable(initial);
        }
Esempio n. 23
0
        static void Main(string[] args)
        {
            int[] size = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int   rows = size[0];
            int   cols = size[1];

            matrix = new BigInteger[rows, cols];

            int[] endPoint = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int   endRow   = endPoint[0];
            int   endCol   = endPoint[1];

            int numberOfEnemies = int.Parse(Console.ReadLine());

            for (int i = 0; i < numberOfEnemies; i++)
            {
                int[] line       = Console.ReadLine().Split().Select(int.Parse).ToArray();
                int   curretnRow = line[0];
                int   currentCol = line[1];

                matrix[curretnRow, currentCol] = -1;
            }


            paths = FindAllPaths(endRow, endCol);
            Console.WriteLine(paths);
        }
Esempio n. 24
0
        static void FillTheMatrix(int n, BigInteger[,] matrix, int[] dRows, int[] dCols)
        {
            int counter = 1;

            for (int rows = 0; rows < n; rows++)
            {
                for (int cols = 0; cols < n; cols++)
                {
                    int r = rows;
                    int c = cols;
                    while (matrix[r, c] == 0)
                    {
                        matrix[r, c] = counter;
                        counter++;
                        for (int direction = 0; direction < dRows.Length; direction++)
                        {
                            int nextRow = r + dRows[direction];
                            int nextCol = c + dCols[direction];
                            if (nextRow < 0 || nextRow > n - 1 || nextCol < 0 || nextCol > n - 1)
                            {
                                continue;
                            }
                            if (matrix[nextRow, nextCol] != 0)
                            {
                                continue;
                            }
                            r = nextRow;
                            c = nextCol;
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 25
0
 public ModMatrix(int d, BigInteger mod)
 {
     dimension = d;
     modulus   = mod;
     m         = new BigInteger[dimension, dimension];
     UnitMatrix();
 }
        private BigInteger[,] BigPowMatrix(BigInteger[,] a, long n)
        {
            // Initialize : Unit Matrix
            var result = new BigInteger[a.GetLength(0), a.GetLength(1)];

            for (var i = 0; i < a.GetLength(0); i++)
            {
                result[i, i] = 1;
            }

            // Exponential Calulation
            var maxDigit = int.MaxValue;

            while (n > 0)
            {
                // Calculate required Digit
                var digit = maxDigit;

                // base check
                if ((n & 0x01) == 1)
                {
                    result = BigMulMatrix(a, result, digit);
                }
                a = BigMulMatrix(a, a, digit);
                // Shift
                n >>= 1;
            }

            return(result);
        }
Esempio n. 27
0
        static BigInteger Dfs(string[] map, BigInteger[,] memo, int row, int col)
        {
            if (row >= map.Length || col >= map[row].Length)
            {
                return(0);
            }

            if (row == map.Length - 1 &&
                col == map[row].Length - 1)
            {
                //Console.WriteLine("Found a way");
                return(1);
            }

            if (map[row][col] != ' ')
            {
                return(0);
            }

            if (memo[row, col] < 0)
            {
                var down  = Dfs(map, memo, row + 1, col);
                var right = Dfs(map, memo, row, col + 1);
                memo[row, col] = down + right;
            }

            return(memo[row, col]);
        }
Esempio n. 28
0
    static void Main()
    {
#if DEBUG
        Console.SetIn(new System.IO.StreamReader("../../input.txt"));
#endif

        input = Console.ReadLine().ToCharArray();

        dp = new BigInteger[input.Length, input.Length];

        for (int row = 0; row < dp.GetLength(0); row++)
            for (int col = 0; col < dp.GetLength(1); col++)
                dp[row, col] = -1;

        Console.WriteLine(Variations(0, 0));

#if DEBUG
        for (int row = 0; row < dp.GetLength(0); row++)
        {
            for (int col = 0; col < dp.GetLength(1); col++)
                Console.Write("{0, 3} ", dp[row, col]);

            Console.WriteLine();
        }
#endif
    }
Esempio n. 29
0
        private static void FillMatrix(BigInteger[,] matrix)
        {
            var rows = matrix.GetLength(0);
            var cols = matrix.GetLength(1);

            matrix[rows - 1, 0] = 1;

            for (int i = rows - 2; i >= 0; i--)
            {
                matrix[i, 0] = matrix[i + 1, 0] * 2;
            }

            for (int i = 1; i < cols; i++)
            {
                matrix[rows - 1, i] = matrix[rows - 1, i - 1] * 2;
            }

            for (int row = rows - 2; row >= 0; row--)
            {
                for (int col = 1; col < cols; col++)
                {
                    matrix[row, col] = matrix[row + 1, col] + matrix[row, col - 1];
                }
            }
        }
Esempio n. 30
0
        private static BigInteger CountHappyTickets(BigInteger[,] happyTickets, int len, int sum)
        {
            if (happyTickets[len, sum] >= 0)
            {
                return(happyTickets[len, sum]);
            }
            if (sum == 0)
            {
                return(1);
            }
            if (len == 0)
            {
                return(0);
            }

            happyTickets[len, sum] = 0;
            for (var i = 0; i < 10; i++)
            {
                if (sum - i >= 0)
                {
                    happyTickets[len, sum] += CountHappyTickets(happyTickets, len - 1, sum - i);
                }
            }

            return(happyTickets[len, sum]);
        }
Esempio n. 31
0
        private static BigInteger GetRouteSum(BigInteger[,] matrix, int currentRow, int currentCol, int targetRow, int targetCol)
        {
            BigInteger totalSum = 0;

            while (currentRow != targetRow || currentCol != targetCol)
            {
                if (targetCol > currentCol)
                {
                    currentCol++;
                }
                else if (targetCol < currentCol)
                {
                    currentCol--;
                }
                else if (targetRow > currentRow)
                {
                    currentRow++;
                }
                else if (targetRow < currentRow)
                {
                    currentRow--;
                }

                totalSum += matrix[currentRow, currentCol];
                matrix[currentRow, currentCol] = 0;
            }

            return(totalSum);
        }
Esempio n. 32
0
        public Binomial(int max, int lookup)
        {
            this.max = max;

            ps = new PrimeSieve(this.max);

            if (lookup < 10)
            {
                lookup = 10;
            }
            this.lookupLimit = lookup;

            int lookup2 = lookup / 2;

            binomialLookup = new BigInteger[lookup + 1, lookup2 + 1];

            for (int total = 1; total <= lookup; total++)
            {
                for (int choose = 0; choose <= total / 2; choose++)
                {
                    //try {
                    binomialLookup[total, choose] = Generate(choose, total);
                    //Console.WriteLine("Binomial " + total + " choose " + choose + " is " + binomialLookup[total, choose]);
                    //} catch (Exception e) {
                    //    throw e;
                    //}
                }
            }
        }
        static BigInteger MoveToCol(int startRow, int startCol, int endCol, BigInteger[,] matrax)
        {
            BigInteger colSum = 0;

            if (startCol < endCol)
            {
                for (int i = startCol; i <= endCol; i++)
                {
                    colSum += matrax[startRow, i];
                    matrax[startRow, i] = 0;
                }

                return(colSum);
            }
            else if (startCol > endCol)
            {
                for (int i = startCol; i >= endCol; i--)
                {
                    colSum += matrax[startRow, i];
                    matrax[startRow, i] = 0;
                }

                return(colSum);
            }

            return(colSum);
        }
        static BigInteger MoveToRow(int startRow, int endRow, int endCol, BigInteger[,] matrix)
        {
            BigInteger rowSum = 0;

            if (startRow > endRow)
            {
                for (int i = startRow; i >= endRow; i--)
                {
                    rowSum           += matrix[i, endCol];
                    matrix[i, endCol] = 0;
                }
                return(rowSum);
            }
            else if (startRow < endRow)
            {
                for (int i = startRow; i <= endRow; i++)
                {
                    rowSum           += matrix[i, endCol];
                    matrix[i, endCol] = 0;
                }

                return(rowSum);
            }

            return(rowSum);
        }
Esempio n. 35
0
        private static void Input()
        {
            // On the first line there will be the numbers N and M, separated by a single space.
            var line = Console.ReadLine().Split(' ');

            n     = int.Parse(line[0]);
            m     = int.Parse(line[1]);
            field = new BigInteger[n, m];

            // On the second line there will be the integer numbers Fx and Fy, separated by a single space.
            line = Console.ReadLine().Split(' ');
            fx   = int.Parse(line[0]);
            fy   = int.Parse(line[1]);

            // On the third line there will be the number K – the number of Doge`s enemies. Many enemies.
            var k = int.Parse(Console.ReadLine());

            for (int i = 0; i < k; i++)
            {
                // On the next K lines there will be the X and Y coordinates for each Doge`s enemy, separated by a space.
                line = Console.ReadLine().Split(' ');
                var enemyX = int.Parse(line[0]);
                var enemyY = int.Parse(line[1]);

                field[enemyX, enemyY] = Enemy;
            }
        }
Esempio n. 36
0
        public void GenerateTable(int power, List<int> polynom)
        {
            int size = (int)Math.Pow(2, power);
            Ttable = new BigInteger[size, size];

            LFSRInfo valueSets = LFSR.GenerateAllSets(polynom, power);
            for (int i = 0; i < valueSets.Sets.Count - 1; i++)
            {
                Ttable[GenerateIndex(valueSets.Sets[i + 1]),GenerateIndex(valueSets.Sets[i])] = 1;
            }
        }
Esempio n. 37
0
        public void GenerateTable(int size)
        {
            Ctable = new BigInteger[size, size];

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Ctable[i, j] = GetCValue(i, j);
                }
            }
        }
Esempio n. 38
0
    static void Input()
    {
        string[] sizeOfMatrix = Console.ReadLine().Split(new char[] { ' ' });
        matrix = new BigInteger[int.Parse(sizeOfMatrix[0]), int.Parse(sizeOfMatrix[1])];

        string[] boneCoordinates = Console.ReadLine().Split(new char[] { ' ' });
        fx = int.Parse(boneCoordinates[0]);
        fy = int.Parse(boneCoordinates[1]);

        int countOfEnemys = int.Parse(Console.ReadLine());
        for (int i = 0; i < countOfEnemys; i++)
        {
            string[] enemyCoordinates = Console.ReadLine().Split(new char[] { ' ' });
            matrix[int.Parse(enemyCoordinates[0]), int.Parse(enemyCoordinates[1])] = -1;
        }
    }
Esempio n. 39
0
        private static void ReadInput()
        {
            var line = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s)).ToArray();
            matrix = new BigInteger[line[0], line[1]];
            line = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s)).ToArray();
            fx = line[0];
            fy = line[1];

            var length = int.Parse(Console.ReadLine());

            for (int i = 0; i < length; i++)
            {
                line = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s)).ToArray();

                matrix[line[0], line[1]] = -1;
            }
        }
Esempio n. 40
0
        private static void ReadInput()
        {
            var input = Console.ReadLine().Split().ToArray();
            n = int.Parse(input[0]);
            m = int.Parse(input[1]);
            labyrinth = new BigInteger[n + 1, m + 1];

            input = Console.ReadLine().Split().ToArray();
            var Fx = int.Parse(input[0]);
            var Fy = int.Parse(input[1]);
            labyrinth[Fx + 1, Fy + 1] = Food;

            var enemyCount = int.Parse(Console.ReadLine());

            for (var i = 0; i < enemyCount; i++)
            {
                input = Console.ReadLine().Split().ToArray();
                var Ex = int.Parse(input[0]);
                var Ey = int.Parse(input[1]);
                labyrinth[Ex + 1, Ey + 1] = Enemy;
            }
        }
Esempio n. 41
0
        static void Input()
        {
            // Matrix Size
            var matrixSize = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse)
                .ToArray();

            // Food coordinates.
            var posFood = Console.ReadLine()
                .Split(' ')
                .Select(int.Parse)
                .ToArray();

            foodRow = posFood[0] + 1;
            foodCol = posFood[1] + 1;

            // Dynamic Programming
            dpMatrix = new BigInteger[foodRow, foodCol];
            matrix = new bool[foodRow, foodCol];

            // Enemies
            var numEnemies = int.Parse(Console.ReadLine());

            for (int enemies = 0; enemies < numEnemies; enemies++)
            {
                var posEnemy = Console.ReadLine()
                    .Split(' ')
                    .Select(int.Parse)
                    .ToArray();

                if (posEnemy[0] < foodRow && posEnemy[1] < foodCol)
                {
                    matrix[posEnemy[0], posEnemy[1]] = true;
                }
            }
        }
Esempio n. 42
0
    private static void Initialization(int totalNumRows, int totalNumCols)
    {
        checkMatrix = new bool[totalNumRows, totalNumCols];
        sum = 0;
        matrix = new BigInteger[totalNumRows, totalNumCols];
        currentCol = 0;
        currentRow = matrix.GetLength(0) - 1;

        BigInteger currentCell = 1;
        for (int i = matrix.GetLength(0) - 1; i >= 0; i--)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                matrix[i, j] = currentCell;
                currentCell *= 2;
            }

            currentCell = matrix[i, 1];
        }
    }
Esempio n. 43
0
        static void Main()
        {
            //var reader = new StreamReader("../../input/3.txt");
            //Console.SetIn(reader);

            size = int.Parse(Console.ReadLine());
            board = new BigInteger[size, size];
            int[] position = new int[2];
            int[] targetPosition = new int[2];

            if (size <= 2)
            {
                Console.WriteLine("No");
                return;
            }

            for (int row = 0; row < size; row++)
            {
                var line = Console.ReadLine()
                    .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                    .ToArray();

                for (int col = 0; col < size; col++)
                {
                    var val = line[col];
                    BigInteger numericVal = ulong.MaxValue;

                    if (val == "x")
                    {
                        numericVal = -1;
                    }
                    else if (val == "s")
                    {
                        position = new int[] { row, col };
                        startPosition = new int[] { row, col };
                    }
                    else if (val == "e")
                    {
                        numericVal = -2;
                        targetPosition = new int[] { row, col };
                    }

                    board[row, col] = numericVal;
                }
            }

            FindPaths(board, position);

            if (bestPath != ulong.MaxValue)
            {
                Console.WriteLine(bestPath);
            }
            else
            {
                Console.WriteLine("No");
            }
        }