unable to understand the row or column sum matrix problem - c

I am new here can anyone solve this problem because I didn't understand this question which column and row sum happen???
Row or Column sum
John likes to play with numbers. He is playing a game where he writes numbers starting 1 till N in multiple rows. Initially he chooses a number W and he writes W numbers in each row except probably in the last row when he is done writing till number N. Given N and W and any row or column in this arrangement, print the sum of all the numbers in this row or column.
Input
First line of input will contain a number T = number of test cases. For each test case, the first line will contain two numbers N and W. Another line will contain a number X and letter 'R' or 'C' separated by space. If letter is 'R', print the sum of numbers in row number X , else if letter is 'C', print the sum of numbers in column number X. Rows and columns are numbered in 1-based index ( as 1,2,3,..)
Output
For each test case, print the sum of numbers in the row or column on a single line.
Sample Input
2
5 2
2 C
10 3
4 R
Sample Output
6
10

Related

Take two integers a,b and print all the even numbers in between them, excluding the input integers

Take two integers a,b and print all the even numbers in between them, excluding the input integers.
Constraints:
0 <= a < b <= 100
Input:
Two integers each in a new line.
Output:
Each line in the output contains an even integer between a,b in ascending order.
Example:
Input:
3
12
Output:
4
6
8
10
I could not find it's logic.
I don't get the question that well, but here's what I got out of it:
for i in range(a + 1, b):
if i % 2 == 0:
print(i)
This loop goes through every letter between your 2 constraints (a and b), checks if its divisible by 2, and if it is (which means it's even), it prints it.

Can I sum integer input from terminal without saving the input as a variable?

I'm trying to write a code for digital root of an extremely big number and can't save it as a variable. Is it possible to do without it?
What you're looking to do is to repeatedly add the digits of a number until you're left with a single digit number, i.e. given 123456, you want 1 + 2 + 3 + 4 + 5 + 6 = 21 ==> 2 + 1 = 3
For a number with up to 50 million digits, the sum of those digits will be no more than 500 million which is well within the range of a 32-bit int.
Start by reading the large number as a string. Then iterate over each character in the string. For each character, verify that it's a character digit, i.e. between '0' and '9'. Convert that character to the appropriate number, then add that number to the sum.
Once you've done that, you've got the first-level sum stored in an int. Now you can loop through the digits of that number using x % 10 to get the lowest digit and x / 10 to shift over the remaining digits. Once you've exhausted the digits, repeat the process until you're left with a value less than 10.

How to build a binary matrix from sums

I have two decimal number variables, colSum and rowSum, using those I want to build a matrix of binary values based on those sums, the rowSum array variable is the result of adding all the 1's for each row, the same goes for colSum array.
So ,if you have
rowSum = [0,1,2]
colSum = [1,1,1]
you will have to build properly the following array
matrix = [
[0,0,0],
[0,0,1],
[1,1,0]
]
I'm using this method in PHP, that works for a 3x3 matrix, but not for a bigger one, like 8x8.
First ,fill all the 1's in the rows using the rowSum value.
Then ,try to find a wrong sum value of 2 columns, with a pivot I inter-change them (1 with a cero value) in the same row, until i get the correct value of colSum.
But it will not work because I need some control of the criteria to change the 1 and 0 in the same row for two columns...
This is the method I'm using.
Let's say we have this Matrix (N=3 -> NxN):
0 0 0
0 0 1
1 1 0
then we have the following arrays
R0 = {0,1,2} //--> result of sums of each rows: ( 0+0+0, 0+0+1 , 1+1+0 )
C0 = {1,1,1} // ->sums of each columns
Step 1
Create and fill a NxN array using as many 1's as R0(i) in each row:
0 0 0
1 0 0
1 1 0
compute sums of this new matrix now:
R1 = {0,1,2}
C1 = {2,1,0}
Step 2
Check if for all the elements of the column sums of the created matrix has the same value as C0 (origin)
for ( i=0, N-1) do
if C0(i)!=C1(i) then
ReplaceColumn(i)
end
end
To replace a column we have to dig inside the conditions.
C0(0) = 1 != C1(0) = 2
the first column sum does meet the condition to call the replace ,so
Step 3
Choose criteria for apply the branch & bound method and find the best row to change column that satisfy the global condition (all column sums).
The amount of changes for a difference between columns sums is:
|C0(i)-C1(i)|
for this example, |C0(0)-C1(0)| = 1 change.
Go back condition must be if the change generates a greater difference between the total sum of columns.
Σi,N(|C0(i)-C1(i)|)
So, could this method really work?
Is the goal to construct the matrix that satisfies the row and column sums or a matrix that satisfies them? It's not clear from the question, but if it's the former ("the" case) then it's not going to possible.
Suppose it were the case that you could uniquely represent any m × m matrix of bits in this way. Then consider the following hypothetical compression algorithm.
Take 22n bits of data
Treat it as 2n × 2n bits
To describe the data, use 2 × 2n row and column sums, each using at most log2(2n) = n bits
The data is compressed to 2 × n × 2n bits
Since 2 × n × 2n << 22n and this process could just keep being repeated, the supposition that you can uniquely represent any m × m matrix of bits by only its row and column sums is false.

Multiplying matrix columns

I have a matrix with n rows and 3 columns, and I should multiply row n column 2 with row n column 3.
So if I have a matrix that looks like this:
1 2 3
4 5 6
7 8 9
Then I should multiply 2 with 3, 5 with 6 and 8 with 9, and create a matrix or an array that holds results:
6
30
72
How can I do that in C?
Since you are interested in learning C, an outline should do :-) The output is going to be a single column vector. Input to your function is a matrix, of some dimension p x q, and two column numbers c1 and c2. You can not skin it at least two ways.
a function that does exactly what your problem asks, iterating x[1..p][c1] and x[1..p][c2] (so loop variable will be row numbers 1..p, and multiply them, producing result[1..p]
a function that returns a column vector from a given matrix, and then another function that does the element-wise product of two vectors as above. This jimho might be a more interesting option.
HTH

find unique adjacent indices in 2d array

Assume I have a 2d array of objects, N x N. Assume that a pair can be made of every adjacent pair of objects, horizontally, vertically or diagonally. How can I count how many unique pairs there are for any value of N?
For example for N = 2
0 1
2 3
You can get 01 02 03 21 23 31, note that 03 is the same as 30
Is there a formula to determine how many of these pairs there are for a given N, and even better an algorithm for generating these?
Language is not that important but I will be using c++.
Using the below algorithm and eliminating duplicate indices, I get following counts. Not sure what the formula is yet.
For size N=2
Unique pairs is =6
For size N=3
Unique pairs is =20
For size N=4
Unique pairs is =42
For size N=5
Unique pairs is =72
For size N=6
Unique pairs is =110
For size N=7
Unique pairs is =156
For size N=8
Unique pairs is =210
For size N=9
Unique pairs is =272
Interesting, the formula appears to be 2^2+2, 4^2+4, 6^2+6, 8^2+8 ...
I find it easiest to pick a representative object of each type of pair (in other words, the top object of a vertical pair, the left most of a horizontal pair, and take your pick for diagonal pairs). This gives n(n-1) vertical pairs, n(n-1) horizontal pairs, and 2(n-1)^2 diagonal pairs (equal amounts of each variety). That totals up to 2(n-1)(n+n-1)=2(n-1)(2n-1), in agreement with your guess.
Each row has n-1 intra-row pairs and there are n rows.
Each column has n-1 intra-column pairs and there are n columns.
Each adjacent pair of rows have 2*(n-1) diagonal pairs and there are (n-1) adjacent row pairs.
Multiply and add these numbers and you will get your solution.
Here's the fixed formula for counting unique pairs.
(4 C 2)*(N-1)^2 - 2*(N-2)*(N-1)
Basically you just use the approach in dasblinkenlight's answer and subtract the "duplicate" edges. The duplicate edges will always be the edges between quadrants. I've added an explanation for the counting of duplicates below.
Using the original formula (4 C 2) * (N-1)**2 for N > 2, you will count duplicates. What you want to do is subtract these duplicate edges from the calculation.
Let's examine the simplest cases for N > 2. Suppose N = 3.
0-1-2
|x*x|
3*4*5
|x*x|
6-7-8
See the places where I marked an asterisk instead of an edge? Those edges will be counted twice by the formula. We can calculate them by breaking them up into horizontal and vertical edges. The number of vertical edges that are counted twice will be (N-2)*(N-1).
In the case of N=3, this will be 1 * 2 = 2. The number of horizontal edges that are counted twice will also be (N-2)*(N-1).
So if we simply add up the number of duplicate vertical edges and duplicate horizontal edges, we get
(N-2)*(N-1) + (N-2)*(N-1) = 2*(N-2)*(N-1)
We can simply subtract that number from our total to get the right number of edges.
Testing count in Python:
from math import factorial
def choose(n, k):
return factorial(n)/(factorial(k) * factorial(n-k))
for N in range(2, 10):
print choose(4, 2) * (N-1)**2 - 2 * (N-2) * (N-1)
The program prints:
6
20
42
72
110
156
210
272

Resources