Weird question I guess.. But I am not very math wiz - soo here goes..
I am trying to create a patterne (or variable patterns based on selection) based on x and y numbers (2 rows and 4 columns) and the direction of the counting of x numbers
All in PHP ;-)
like:
1-2-3-4
5-6-7-8
That one is easy, when number of x-columns is reached, next line and continue x count.
But with eg. this one (still 2 rows and 4 columns):
1-2-3-4
8-7-6-5
upsie.. what if it is eg. 3++ rows and still 4 columns?
1-2-3-4
8-7-6-5
9-10-11-12
what would be the formula for this - or other possible variations (teaser for variations):
9-10-11-12
8-7-6-5
1-2-3-4
or reversed
For the example you gave where every 2nd row is reversed, use:
int x;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
if (i % 2 == 0) // Rows with even index
x = (i * columnCount) + (j+1);
else // Other rows (i.e. odd index)
x = ((i+1) * columnCount) - (j+1);
}
}
You haven't specified a programming language, but the answer looks roughly the same regardless.
Related
count[0] = 1;
for (int x = 1; x <= n; x++) {
for (auto c : coins) {
if (x-c >= 0) {
count[x] += count[x-c];
}
}
}
count[0] = 1;
for (auto c : coins) {
for (int x = 1; x <= n; x++) {
if (x-c >= 0) {
count[x] += count[x-c];
}
}
}
The first piece of code gives the number of possible permutations of each amount, whereas the second gives the combinations, why is this is the case?
The second code updates number of ways to compose values count[i] with new coin. So after k-th round of outer loop we have count[] list filled with combinations of k coins only, and the rest of coins is not used yet. If we remember combinations themselves for sorted coin list, we will see ordered ones like 1 1 5 10 - 5 never will go before 1. That is why combinations.
The first code at m-th round of outer loop fills m-th cell count[m] using all possible coins. So we can get for m=7 both 1 1 5 and 1 5 1 and 5 1 1. That is why all permutations are counted here
Given a n*m matrix, a cell is called good cell if row number (i) divides column number (j)
Example :
2*3 matrix => cells are {1,1}, {1,2}, {1,3}, {2,1}, {2,2}, {2,3} from which good cells can be defined as {1,1}, {1,2}, {1,3}, {2,2}
So the output is 4
I have figure out the logic for this, row 1 has all cells as good cells, row 2 has m/2 good cells, row 3 has m/3 good cells. (I am using just the integer dividend)
m/1 + m/2 + m/3 + ........ + m/n;
For which my code looks like =>
long count=1;
long answer = 0;
while(count<=n){
answer=answer + m/count;
count++;
}
But this code is getting timed out when I am submitting my solution. Need help to find better approach for this.
PS: Coding challenge is already over.
Try this one,
for(int i = 0 ; i < n; i++)
{
for(int j = 0; j < m; j += i) // j += i will save unwanted loops
{
// rest of your logic
}
}
As the value of i gets higher and higher nested loop will become more efficient
edit
The below code will be more efficient & correct than above. Now nested loop starts with the value of i instead of 0
for(int i = 0 ; i < n; i++)
{
for(int j = i; j < m; j += i) //j = i & j += i will save unwanted loops
{
// rest of your logic
}
}
as noted by n pronouns m this post incorrect since it only considers the case n==m
You may have a look at the Dirichlet divisor problem
One attempt is the formula of Benoit Cloitre:
(below octave implem)
function s = sumit(n)
s = 0;
for i = 1:n
s += floor(n/i);
end
end
n = 1e6
expect = sumit(n)
%A006218
u = floor(sqrt(n));
res = 2*sum(floor(n./(1:u))) - u^2
Where you avoid summing quite some terms.
// A Dynamic programming based C++ program to find number of
// non-negative solutions for a given linear equation
#include<bits/stdc++.h>
using namespace std;
// Returns counr of solutions for given rhs and coefficients
// coeff[0..n-1]
int countSol(int coeff[], int n, int rhs)
{
// Create and initialize a table to store results of
// subproblems
int dp[rhs+1];
memset(dp, 0, sizeof(dp));
dp[0] = 1;
// Fill table in bottom up manner
for (int i=0; i<n; i++)
for (int j=coeff[i]; j<=rhs; j++)
dp[j] += dp[j-coeff[i]];
return dp[rhs];
}
// Driver program
int main()
{
int coeff[] = {2, 2, 5};
int rhs = 4;
int n = sizeof(coeff)/sizeof(coeff[0]);
cout << countSol(coeff, n, rhs);
return 0;
}
I am new to competitive programming, I just stumbled upon this code. I would like to know the intuition behind this particular snippet, like how does the second for loop help. Thank you.
// Fill table in bottom up manner
for (int i=0; i<n; i++)
for (int j=coeff[i]; j<=rhs; j++)
dp[j] += dp[j-coeff[i]];
This is using a bottom up approach, and
Suppose if j= 3 and j-coeff[i] = 2
so how does d[3] = d[3] + d[2] give the solution? How can a simple addition of a previous result and a current result give the total solution of linear variables?
Imagine that you have unlimited number of coins with value 2,3,5 (your coeff[]) and you want to know number of solutions to make some sum form give coin set.
At the first loop run you fill table with coins 2. Table will be filled
idx 0 1 2 3 4 5 6
num 1 0 1 0 1 0 1
because there is the only way to get even sum with such coins.
At the second loop run you fill table with coins 3 - now you'll have sums that might be composed from coins 2 and 3
idx 0 1 2 3 4 5 6
num 1 0 1 1 1 1 2
Note that cell 5 filled with 2+3 - similar to your question situation, and cell 6 now contains 2 variants: 2+2+2 and 3+3
I was trying to solve the "DZY Loves Modification" problem from Round #255 of CodeForces.
Problem link:
http://codeforces.com/contest/446/problem/B.
I am getting wrong answers for the system test cases.
My approach is as follows:
Construct two max-heaps - one heap for storing the row totals and
one for the column totals
Store two variables - rowReductionValue and colReductionValue -
these variables store the value that should be subtracted before
using a row or column's total sum.
For each iteration, choose the entry with maximum value from the
rowSum and colSum heaps
If a row is chosen, the row's total is added to the result and then
the row total is reduced by P * (number_of_columns)
After this, each column's value reduces by P. So, this P is
added to colReductionValue.
An analogous approach is used if a column is chosen.
This approach results in wrong answers.
I am sorry if I was not clear in explaining my approach, I wanted it to be as concise as possible.
Any light on the right approach or the flaw with this approach is much appreciated.
The code is below:
long long getMaxResult(int rows, int cols, int reductionValue, int K)
{
vector<long long> rowSums, colSums;
long long rowReductionValue = 0, colReductionValue = 0, result = 0;
for(int row = 0; row < rows; ++row)
{
long long sum = 0;
for(int col = 0; col < cols; ++col)
{
sum += a[row][col];
}
rowSums.push_back(sum);
}
for(int col = 0; col < cols; ++col)
{
long long sum = 0;
for(int row = 0; row < rows; ++row)
{
sum += a[row][col];
}
colSums.push_back(sum);
}
make_heap(rowSums.begin(), rowSums.end());
make_heap(colSums.begin(), colSums.end());
for(int k = 0; k < K; ++k)
{
pop_heap(rowSums.begin(), rowSums.end());
long long rowMax = rowSums.back();
rowSums.pop_back();
pop_heap(colSums.begin(), colSums.end());
long long colMax = colSums.back();
colSums.pop_back();
if(rowMax - rowReductionValue >= colMax - colReductionValue)
{
result += rowMax - rowReductionValue;
rowMax -= reductionValue * cols;
colReductionValue += reductionValue;
}
else
{
result += colMax - colReductionValue;
colMax -= reductionValue * rows;
rowReductionValue += reductionValue;
}
rowSums.push_back(rowMax);
push_heap(rowSums.begin(), rowSums.end());
colSums.push_back(colMax);
push_heap(colSums.begin(), colSums.end());
}
return result;
}
Thanks-
I'll walk you through the Greedy-Brute Force concept that I used to approach this problem:
If p = 0, apparently the best choice is choosing the row or column which can give greatest pleasure value each time.
Let's ignore p first, then we can get a greatest number ans using previous statement. Now if we choose rows for i times, choose column for k-i times, ans should be ansi - ((k-i) X i X p).
So we could enumerate i from 0 to k and calculate ansi - ((k-i) X i X p) each time, max{ansi - ((k-i) X i X p)} is the maximum possible pleasure value DZY could get.
Let ai be the maximum pleasure value we can get after choosing i rows and bi be the maximum pleasure value we can get after choosing i columns. Then ansi = ai+bk-i. We can use two priority queues to calculate ai and bi quickly.
Good luck!
I have been trying to formulate an algorithm to solve a problem. In this problem, we have a photo containing some buildings. The photo is divided into n vertical regions (called pieces) and the height of a building in each piece is given.
One building may span several consecutive pieces, but each piece can only contain one visible building, or no buildings at all. We are required to find the minimum number of buildings.
e.g.
given ,
3 ( no of pieces)
1 2 3 ( heights) ans = 3
3
1 2 1 ans = 2
6
1 2 3 1 2 3 ans = 5 ( a figure wud help show the overlap.).
Though I feel like I get it, I am unable to get a solid algorithm for it. Any ideas?
You can find the lowest number from the given array and account for all occurances of this number. This will split the array into multiple subarrays and now you need to recursively solve the problem for each of them.
In the example:
1 2 3 1 2 3 (total = 0)
Smallest number is 1:
x 2 3 x 2 3 (total = 1)
Now you have 2 subarrays.
Solve for the first one - the smallest number is 2:
x 3 (total = 2)
Finally you have a single element: total = 3
Solving the other subarray makes it 5.
Here is some code in C#:
int Solve(int[] ar, int start, int end){
//base for the recursion -> the subarray has single element
if(end-start == 1) return 1;
//base for the recursion -> the subarray is empty
if(end-start < 1) return 0;
//find min
int m = int.MaxValue;
for(int i = start; i < end; i++)
if (ar[i] < m) m = ar[i];
int total = 1;
//find the subarrays and their contribution recursively
int subStart = start;
for(int subEnd = start; subEnd < end; subEnd++){
if(ar[subEnd] == m) {
total += Solve(ar, subStart, subEnd);
subStart = subEnd + 1;
}
}
total += Solve(ar, subStart, ar.Length);
return total;
}