/* * Common Denominators * * You will have a list of rationals in the form * { {numer_1, denom_1} , ... {numer_n, denom_n} } * or * [ [numer_1, denom_1] , ... [numer_n, denom_n] ] * or * [ (numer_1, denom_1) , ... (numer_n, denom_n) ] * where all numbers are positive ints. * You have to produce a result in the form * (N_1, D) ... (N_n, D) * or * [ [N_1, D] ... [N_n, D] ] * or * [ (N_1', D) , ... (N_n, D) ] * or * {{N_1, D} ... {N_n, D}} * depending on the language (See Example tests) * in which D is as small as possible and * N_1/D == numer_1/denom_1 ... N_n/D == numer_n,/denom_n. * Example: * convertFracs [(1, 2), (1, 3), (1, 4)] `shouldBe` [(6, 12), (4, 12), (3, 12)] */ public static string ConvertFrac(long[,] lst) { long scd = 1; for (long i = 1; i < lst.GetLength(0); i++) { if (!(scd % lst[i, 1] == 0 && scd % lst[i - 1, 1] == 0)) { scd = SCD(lst[i, 1], lst[i - 1, 1]); lst[i - 1, 0] = scd / lst[i - 1, 1] * lst[i - 1, 0]; lst[i, 0] = scd / lst[i, 1] * lst[i, 0]; lst[i - 1, 1] = lst[i, 1] = scd; } } StringBuilder sb = new StringBuilder(); for (long i = 0; i < lst.GetLength(0); i++) { if (lst[i, 1] != scd) { lst[i, 0] = scd / lst[i, 1] * lst[i, 0]; lst[i, 1] = scd; } sb.Append(string.Format("({0},{1})", lst[i, 0], lst[i, 1])); } return(sb.ToString()); }
private static void FillSums() { for (int row = 0; row < cells.GetLength(0); row++) { for (int col = 0; col < cells.GetLength(1); col++) { long maxPrevCell = long.MinValue; if (col > 0 && sum[row, col - 1] > maxPrevCell) { maxPrevCell = sum[row, col - 1]; } if (row > 0 && sum[row - 1, col] > maxPrevCell) { maxPrevCell = sum[row - 1, col]; } sum[row, col] = cells[row, col]; if (maxPrevCell != long.MinValue) { sum[row, col] += maxPrevCell; } } } }
public static string convertFrac(long[,] lst) { if (1 >= lst.GetLength(1)) { return(""); } var gcd = lst[0, 1]; var lcm = gcd; for (var i = 1; i < lst.GetLength(0); i++) { gcd = GetGreatestCommonDenominator(lcm, lst[i, 1]); lcm = lcm * lst[i, 1] / gcd; } var sb = new StringBuilder(); for (var i = 0; i < lst.GetLength(0); i++) { var divisionDifference = lcm / lst[i, 1]; sb.Append($"({lst[i, 0] * divisionDifference},{lst[i, 1] * divisionDifference})"); } return(sb.ToString()); }
static bool InRange(long[,] tiles, int row, int col) { bool rowInRange = row >= 0 && row < tiles.GetLength(0); bool colInRange = col >= 0 && col < tiles.GetLength(1); return(rowInRange && colInRange); }
/// <summary> /// Merges another instance of <see cref="CountMinSketch{T}"/> with this collection. The results will be an aggregate of both collections after merging. /// </summary> /// <param name="other">The <see cref="CountMinSketchBase{T}"/> that should be merged into the current collection.</param> /// <returns>This <see cref="CountMinSketch{T}"/> with the results from the other collection included.</returns> public override CountMinSketchBase <T> MergeInPlace(CountMinSketchBase <T> other) { if (other == null) { throw new IncompatibleMergeException("Cannot merge null estimator"); } if (other.Depth != Depth) { throw new IncompatibleMergeException("Cannot merge estimators with different depths"); } if (other.Width != Width) { throw new IncompatibleMergeException("Cannot merge estimators with different widths"); } if (other.Seed != Seed) { throw new IncompatibleMergeException("Cannot merge sketches that were initialized with different seeds"); } for (var i = 0; i < _table.GetLength(0); i++) { for (var j = 0; j < _table.GetLength(1); j++) { _table[i, j] = _table[i, j] + other.Table[i, j]; } } _totalCount += other.TotalCount; return(this); }
public (long, long, long) FindBestSize(long[,] arr, int start, int end) { long bestSoFar = 0, bestX = 0, bestY = 0, bestSize = 0, xn = arr.GetLength(0), yn = arr.GetLength(1); for (var size = start; size <= end; size++) //foreach size { for (var x = 1; x <= xn - (size - 1); x++) //foreach x up to the point where the size wont fit { for (var y = 1; y <= yn - (size - 1); y++) //foreach x up to the point where the size wont fit { var cellPower = GetPoweCellSumAreaTable(x, y, size, arr); //work out the sum for this square if (cellPower <= bestSoFar) { continue; //skip while no improvement found } bestSoFar = cellPower; bestX = x; bestY = y; bestSize = size; } } } return(bestX, bestY, bestSize); }
static long[,] MultiMat(long[,] left, long[,] right) { int h = left.GetLength(0); int w = right.GetLength(1); int c = left.GetLength(1); if (c != right.GetLength(0)) { return(null); } long[,] mat = new long[h, w]; for (int i = 0; i < h; i++) { for (int ii = 0; ii < w; ii++) { long val = left[i, 0] & right[0, ii]; for (int iii = 1; iii < c; iii++) { val ^= left[i, iii] & right[iii, ii]; } mat[i, ii] = val; //Console.Write(mat[i, ii]+" "); } //Console.WriteLine(); } return(mat); }
private static string Solve(long[,] matrix) { long maxSum = int.MinValue; bool isSumFound = false; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { if (ContainsPattern(matrix, row, col)) { long patternSum = CalculatePatternSum(matrix, row, col); if (maxSum < patternSum) { maxSum = patternSum; } isSumFound = true; } } } if (isSumFound) { return(string.Format("YES {0}", maxSum)); } else { long diagonalSum = 0; for (int index = 0; index < matrix.GetLength(0); index++) { diagonalSum += matrix[index, index]; } return(string.Format("NO {0}", diagonalSum)); } }
public string[] Solve(int V, int E, long[,] matrix) { var variables = V * 3; var constraints = V + matrix.GetLength(0) * 3; List <string> result = new List <string>(); result.Add($"{variables} {constraints}"); for (int v = 0; v < V; v++) { StringBuilder s = new StringBuilder(); for (int j = 1; j <= 3; j++) { s.Append($"{(v*3)+j} "); } s.Append("0"); result.Add(s.ToString()); } for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 1; j <= 3; j++) { result.Add($"-{(matrix[i, 0] - 1) * 3 + j} -{(matrix[i, 1] - 1) * 3 + j} 0"); } } return(result.ToArray()); }
public static string Calculate(long[,] fractions) { var denominators = new List <long>(); int w = fractions.GetLength(0); // width int h = fractions.GetLength(1); // height System.Console.WriteLine($"{w}, {h}"); for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { if (y == 1) { denominators.Add(fractions[x, y]); } } } denominators.Sort(); denominators.Reverse(); System.Console.WriteLine(@"[{0}]", string.Join(", ", denominators)); long lcd = -1; var hasLcd = false; foreach (var den in denominators) { } System.Console.WriteLine(lcd); return(""); }
public static bool isGuardNear(int row, int col, long[,] maze) { // Guard Position // Right if (col + 1 < maze.GetLength(1) && maze[row, col + 1] == -3) { return(true); } // Down if (row + 1 < maze.GetLength(0) && maze[row + 1, col] == -4) { return(true); } // Left if (col - 1 >= 0 && maze[row, col - 1] == -1) { return(true); } // Up if (row - 1 >= 0 && maze[row - 1, col] == -2) { return(true); } return(false); }
public bool Compare(long[,] patternHash, long[,] gridHash, int startRow, int startColumn, int patternRows, int patternColumns) { int lastRow = startRow + patternRows - 1; int lastColumn = startColumn + patternColumns - 1; if (lastRow >= gridHash.GetLength(0) || lastColumn >= gridHash.GetLength(1)) { return(false); } long patternHashValue = patternHash[patternRows - 1, patternColumns - 1] * this.powers[startRow * patternColumns + startColumn] % this.mod; long gridHashValue = gridHash[lastRow, lastColumn]; if (startRow > 0) { gridHashValue = (gridHashValue + this.mod - gridHash[startRow - 1, lastColumn]) % this.mod; } if (startColumn > 0) { gridHashValue = (gridHashValue + this.mod - gridHash[lastRow, startColumn - 1]) % this.mod; } if (startRow > 0 && startColumn > 0) { gridHashValue = (gridHashValue + gridHash[startRow - 1, startColumn - 1]) % this.mod; } return(patternHashValue == gridHashValue); }
private static long[,] MatrixProductMod(long[,] matrixA, long[,] matrixB, long mod) { int aRows = matrixA.GetLength(0); int aCols = matrixA.GetLength(1); int bRows = matrixB.GetLength(0); int bCols = matrixB.GetLength(1); if (aCols != bRows) { throw new Exception("Can't multiply because of incompatible sizes"); } long[,] result = new long[aRows, bCols]; Parallel.For(0, aRows, i => { for (int j = 0; j < bCols; ++j) // each col of B { for (int k = 0; k < aCols; ++k) // could use k < bRows { result[i, j] = (result[i, j] + matrixA[i, k] * matrixB[k, j]) % mod; } } }); return(result); }
static long[,] GetModifiedMatrix(long[,] matrix, int[] targetPixel, long blurAmount) { int rows = matrix.GetLength(0); int cols = matrix.GetLength(1); int targetRow = targetPixel[0]; int targetCol = targetPixel[1]; long[,] modifiedMatrix = new long[rows, cols]; // NB! NEW matrix: original matrix unchanged for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { modifiedMatrix[row, col] = matrix[row, col]; } } // NB!!! modifiedMatrix = matrix => original matrix changes as well // filter boundaries (row +/-1, col +/- 1 away from targer pixel) cannot go outside matrix borders int filterLeft = Math.Max(0, targetCol - 1); // limited by first col int filterRight = Math.Min(targetCol + 1, cols - 1); // limited by last col int filterTop = Math.Max(0, targetRow - 1); // limited by first row int filterBottom = Math.Min(targetRow + 1, rows - 1); // limited by last row // apply blur filter for (int row = filterTop; row <= filterBottom; row++) { for (int col = filterLeft; col <= filterRight; col++) { modifiedMatrix[row, col] += blurAmount; } } return(modifiedMatrix); }
static bool JeMaticeSymetricka(long[,] S) { bool symetrie = false; int i, j; for (i = 0; i < S.GetLength(0); i++) { for (j = 0; j < S.GetLength(1); j++) { long prvni = S[i, j]; long druhy = S[j, i]; if (prvni == druhy) { symetrie = true; } else { symetrie = false; break; } } } Console.WriteLine(symetrie); return(symetrie); }
private static long[,] MultiplyCells(long[,] matrix, Cell position, long radius) { long rowStart = Math.Max(position.Row - radius, 0); long colStart = Math.Max(position.Col - radius, 0); long rowEnd = Math.Min(position.Row + radius, matrix.GetLength(0)); long colEnd = Math.Min(position.Col + radius, matrix.GetLength(1)); long rows = rowEnd - rowStart + 1; long cols = colEnd - colStart + 1; long neibhoringCellsSum = 0; //multiply neibhor cells by target cell for (long i = rowStart; i <= rowEnd; i++) { for (long j = colStart; j <= colEnd; j++) { if ((i != position.Row) || (j != position.Col)) { neibhoringCellsSum += matrix[i, j]; matrix[i, j] *= matrix[position.Row, position.Col]; } } } // multiply the targetCell matrix[position.Row, position.Col] *= neibhoringCellsSum; return(matrix); }
static bool JeHorniTrojuhelnikova(long[,] H) { int i, j; bool je = false; int radky = H.GetLength(0); int sloupce = H.GetLength(1); if (radky == sloupce) // je čtvercová { for (i = 0; i < radky; i++) // řádky { for (j = 0; j < sloupce; j++) // sloupce { if (i <= j) { continue; // diagonála je i == j, horní trojúhelník je i < j // my chceme textovat ten zbytek } else if (H[i, j] == 0) { je = true; } else { break; } } } } Console.WriteLine(je); return(je); }
private static List <string> FindPath() { List <string> path = new List <string>(); int rows = cells.GetLength(0) - 1; int cols = cells.GetLength(1) - 1; while (rows + cols > 0) { if (cols == 0) { path.Add($"[{rows--}, {cols}]"); continue; } if (rows == 0) { path.Add($"[{rows}, {cols--}]"); continue; } if (sum[rows, cols - 1] >= sum[rows - 1, cols]) { path.Add($"[{rows}, {cols--}]"); } else { path.Add($"[{rows--}, {cols}]"); } } path.Add($"[{rows}, {cols}]"); path.Reverse(); return(path); }
private long[,] PowMatrix(long[,] a, long n) { // Initialize : Unit Matrix var result = new long[a.GetLength(0), a.GetLength(1)]; for (var i = 0; i < a.GetLength(0); i++) { result[i, i] = 1; } // Exponential Calulation var maxDigit = long.MaxValue; while (n > 0) { // Calculate required Digit var digit = Digit(a[1, 1]); digit = digit * 3 >= maxDigit ? maxDigit : digit * 3; // base check if ((n & 0x01) == 1) { result = MulMatrix(a, result, digit); } a = MulMatrix(a, a, digit); // Shift n >>= 1; } return(result); }
// function to check if given data is valid graph public bool isValidCompleteGraph(long[,] graph) { long no_of_rows = graph.GetLength(0); long no_of_collumns = graph.GetLength(1); long[] no_of_zeros = new long[no_of_rows]; if (no_of_rows == no_of_collumns) { for (int i = 0; i < no_of_rows; i++) { for (int j = 0; j < no_of_collumns; j++) { if (graph[i, j] == 0 && i != j || i == j && graph[i, j] != 0) // morer than one zero or zero in position ij is not zero { // not valid graph return(false); } } } return(true); } else { return(false); } }
public void putData(long[,] data) { int nSamples = data.GetLength(0); if (nSamples == 0) { return; } int nChans = data.GetLength(1); if (nChans == 0) { return; } ByteBuffer buf = preparePutData(nChans, nSamples, DataType.INT64); long[] rowData; for (int i = 0; i < nSamples; i++) { rowData = getRow <long>(data, i); buf.asLongBuffer().put(rowData); } buf.rewind(); writeAll(buf); readResponse(PUT_OK); }
private static void PrintMatrix(long[,] matrix, int n) { int row2 = 0; for (int row1 = 0; row1 < matrix.GetLength(0); row1++) { for (int col1 = 0; col1 < matrix.GetLength(1); col1++) { if (matrix[row1, col1] == 0) { Console.Write('.'); } else if (matrix[row1, col1] == 1) { Console.Write('*'); } } Console.Write(" "); long result = 0; for (int col2 = n - 1; col2 >= 0; col2--) { Console.Write("{0}", matrix[row2, col2]); result += matrix[row2, col2] * (long)Math.Pow(2, n - 1 - col2); } row2++; Console.WriteLine(" --> {0}", result); Console.WriteLine(); } }
/// <summary> /// The standard constructor. Checks the array dimensions and /// initializes the grid parameters. /// </summary> /// <param name="GlobalID">see <see cref="GridCommons.GlobalID"/></param> /// <param name="CellNeighbours">see <see cref="GridCommons.CellNeighbours"/>;</param> /// <param name="Vertices">see <see cref="GridCommons.Vertices"/>;</param> /// <param name="EdgeTags">see <see cref="GridCommons.EdgeTags"/>;</param> public UnstructuredTetraGrid(long[] GlobalID, double[, ,] Vertices, long[,] CellNeighbours, byte[,] EdgeTags) : this() { if (GlobalID.GetLength(0) != Vertices.GetLength(0)) { throw new ArgumentException("mismatch in dimension 0", "GlobalID,Vertices"); } if (GlobalID.GetLength(0) != CellNeighbours.GetLength(0)) { throw new ArgumentException("mismatch in dimension 0", "GlobalID,CellNeighbours"); } if (Vertices.GetLength(1) != m_GridSimplex.Vertices.GetLength(0)) { throw new ArgumentException("dimension 1; wrong number of vertices per element", "Vertices"); } if (Vertices.GetLength(2) != 3) { throw new ArgumentException("dimension 2; wrong spatial dimension", "Vertices"); } if (CellNeighbours.GetLength(1) != 4) { throw new ArgumentException("dimension 1; wrong number of neighbours", "CellNeighbours"); } base.GlobalID = GlobalID; base.Vertices = Vertices; base.CellNeighbours = CellNeighbours; base.EdgeTags = EdgeTags; }
private static void MoveRightDOWN(long[,] bitshop, ref int row, ref int col, bool[,] passed, ref int times, ref long sum) { for (int i = 0; i < times; i++) { if (passed[row, col] == false) { sum += bitshop[row, col]; passed[row, col] = true; } if (row + 1 > bitshop.GetLength(0) - 1 || col + 1 > bitshop.GetLength(1) - 1) { break; } else { if (i == times - 1) { break; } else { row += 1; col += 1; } } } }
private static List<string> FindPath() { List<string> path = new List<string>(); int row = sum.GetLength(0) - 1; int col = sum.GetLength(1) - 1; while (row + col > 0) { if (col == 0) { path.Add($"[{row--}, {col}]"); continue; } if (row == 0) { path.Add($"[{row}, {col--}]"); continue; } if (sum[row, col - 1] >= sum[row - 1, col]) { path.Add($"[{row}, {col--}]"); } else { path.Add($"[{row--}, {col}]"); } } path.Add($"[{row}, {col}]"); path.Reverse(); return path; }
public static long[,] UpdateMatrix(long[,] matrix) { long[,] resultMatrix = new long[matrix.GetLength(0), matrix.GetLength(1)]; long rowc = 0; long colc = 0; bool updateRow = true; for (long i = 0; i < matrix.GetLength(0); i++) { for (long j = 0; j < matrix.GetLength(1); j++) { if (matrix[i, j] != 0) { resultMatrix[rowc, colc++] = matrix[i, j]; updateRow = true; } } if (updateRow) { rowc++; updateRow = false; } colc = 0; } return(resultMatrix); }
public static string convertFrac(long[,] lst) { if (lst.GetLength(0) <= 0) { return(string.Empty); } long[] denominators = new long[lst.GetLength(0)]; for (int i = 0; i < denominators.Length; i++) { denominators[i] = lst[i, 1]; } long lcm = Fracts.lcm(denominators); long[,] answer = new long[lst.GetLength(0), 2]; for (int i = 0; i < lst.GetLength(0); i++) { long multiplier = lcm / lst[i, 1]; answer[i, 0] = lst[i, 0] * multiplier; answer[i, 1] = lst[i, 1] * multiplier; } return(Fracts.Print2DArray(answer)); }
static List <long> checkSquare(long[,] a) { List <long> output = new List <long>(); long startingNumber = 0; long endingNumber = 0; long count = 0; long y = a.GetLength(0); for (long i = 0; i < a.GetLength(0); i++) { startingNumber = a[i, 0]; endingNumber = a[i, 1]; for (long j = startingNumber; j <= endingNumber; j++) { double sq = Math.Sqrt(j); long x = Convert.ToInt64(Math.Truncate(sq)); if (x * x == j) { count = count + 1; } } output.Add(count); count = 0; } return(output); }
private void Validation() { Lines = StationCost.GetLength(0); Nodes = StationCost.GetLength(1); if (Lines <= 0) { throw new InvalidOperationException("Invalid lines"); } if (Nodes <= 0) { throw new InvalidOperationException("Invalid nodes"); } if (Lines != Enter.Length || Lines != Exit.Length || Lines != SwitchCost.GetLength(0)) { throw new InvalidOperationException("Lines mismatch"); } if (Nodes != SwitchCost.GetLength(1) + 1) { throw new InvalidOperationException("Node count mismatch"); } }
private static long GetLargestVerticalProduct(long[,] grid, long adjacentNumbers) { long largestProduct = 0; long candidate = 1; for (long i = 0; i < grid.GetLength(0); i++) { for (long j = 0; j < grid.GetLength(1); j++) { if (i + adjacentNumbers - 1 < grid.GetLength(0)) { for (long k = i; k < adjacentNumbers; k++) { candidate *= grid[k, j]; } if (candidate > largestProduct) { largestProduct = candidate; } candidate = 1; } } } return(largestProduct); }