summation of power of fibonacci number? [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to find the sum of this series
fib(0)^K + fib(C)^K + fib(2*C)^K + fib(3*C)^K + ... + fib(N*C)^K
where constraint are 0 < N < 10^15 , 0 < C < 11 and 0 < k < 11?
Here fib(i) is the i-th fibonacci number, where fib(0)=0 , fib(1)=1 and fib(n) = fib(n-1)+fib(n-2). We have to calculate the summation modulo 1000000007 (10^9+7).

This is an exercise in manipulating recurrence relations, basically. To understand this answer, you should be comfortable switching between matrix and system form.
First we get a recurrence for fib(i)^K. This actually involves a system of recurrences for fib(i)^K, fib(i)^(K-1) fib(i-1), fib(i)^(K-2) fib(i-1)^2, ..., fib(i-1)^K. I'll demonstrate for K = 3.
fib(i)^3 = (fib(i-1) + fib(i-2))^3
= fib(i-1)^3 + 3 fib(i-1)^2 fib(i-2) + 3 fib(i-1) fib(i-2)^2 + fib(i-2)^3
fib(i)^2 fib(i-1) = (fib(i-1) + fib(i-2))^2 fib(i-1)
= fib(i-1)^3 + 2 fib(i-1)^2 fib(i-2) + fib(i-1) fib(i-2)^2
fib(i) fib(i-1)^2 = (fib(i-1) + fib(i-2)) fib(i-1)^2
= fib(i-1)^3 + fib(i-1)^2 fib(i-2)
fib(i-1)^3 = fib(i-1)^3
These can be combined into a single matrix.
[fib(i)^3 fib(i)^2 fib(i-1) fib(i) fib(i-1)^2 fib(i-1)^3] =
i
= [fib(0)^3 fib(0)^2 fib(-1) fib(0) fib(-1)^2 fib(-1)^3] [1 0 0 0]
[3 1 0 0]
[3 2 1 0]
[1 1 1 1]
i
= [0 0 0 1] [1 0 0 0]
[3 1 0 0]
[3 2 1 0]
[1 1 1 1]
You may recognize Pascal's triangle over there.
Now, given a system of recurrences for a function f(i), we can compute recurrences for f(c i) by raising the matrix to the power c.
The final step is to go from a recurrence for f(i) to a recurrence for F(i) = f(0) + f(1) + ... + f(i-1). It's simple to add an equation
F(i) = F(i-1) + f(i-1)
to the system.
Having computed the matrix, which by my estimate will have at most 12^2 = 144 elements, we can compute the appropriate power using fast matrix exponentiation mod 10^9 + 7. Mind the lurking off-by-one error – that's F(n+1) that you want.

Related

I want an algorithm to this problem. From a matrix m find the matrix r [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
matrix m:
1 2 3
4 5 6
7 8 9
output: (matrix r)
1 3 6
5 12 21
12 27 45
How we get the results is:
staring index = (0,0)
for example,
element at the (1,1) position of the result matrix would be,
r[1][1] = m[0][0] + m[0][1] + m[1][0] + m[1][1]
sum of the elements inside the red box:
element at the (2,1) position of the result matrix would be,
r[2][1] = matrix[0][0] + m[0][1] + m[1][0] + m[1][1] + m[2][0] + m[2][1]
sum of the elements inside the red box:
One important observation here is that for i > 0 and j > 0:
r[i][j] = m[i][j] + r[i-1][j] + r[i][j-1]
^ ^
When i == 0 or j == 0 then just drop the terms from the above expression that become invalid.
So:
r[0][0] = m[0][0]
And:
r[0][1] = m[0][1] + r[0][0]
If you continue with the first row from left to right and then the next rows in the same fashion, you'll always have the information needed to calculate r[i][j].

What is the most efficient algorithm for solving the cell-sum puzzle?

The cell-sum puzzle is defined as follows:
Given two sets of non-negative integers X = {x1, x2,...,xm} and Y = {y1, y2,...,yn}, fill each cell in a grid of m rows and n columns with a single non-negative integer such that xi is the sum of the cells in the ith row for every i ≤ m and such that yj is the sum of the cells in the jth column for every j ≤ n.
For example, if X = {7, 13} and Y = {8, 9, 3}, then your goal would be to replace the question marks in the following grid:
? + ? + ? = 7
+ + +
? + ? + ? = 13
= = =
8 9 3
and a valid solution would be:
3 + 1 + 3 = 7
+ + +
5 + 8 + 0 = 13
= = =
8 9 3
How do you solve this puzzle for arbitrarily large m and n? Also, for your method of choice, do you know the time complexity, and can you tell whether it is the most efficient algorithm possible?
Here's a linear-time algorithm (O(m + n) assuming we can output a sparse matrix, which is asymptotically optimal because we have to read the whole input; otherwise O(m n), which is optimal because we have to write the whole output).
Fill in the upper-left question mark with the min of the first row sum and the first column sum. If the first row sum equals the min, put zeros in the rest of the row. If the first column sum equals the min, put zeros in the rest of the column. Extract the subproblem by subtracting the new value from the first row/column if they remain and recurse.
On your example:
? + ? + ? = 7
+ + +
? + ? + ? = 13
= = =
8 9 3
Min of 7 and 8 is 7.
7 + 0 + 0 = 7
+ + +
? + ? + ? = 13
= = =
8 9 3
Extract the subproblem.
? + ? + ? = 13
= = =
1 9 3
Min of 13 and 1 is 1.
1 + ? + ? = 13
= = =
1 9 3
Extract the subproblem.
? + ? = 12
= =
9 3
Keep going until we get the final solution.
7 + 0 + 0 = 7
+ + +
1 + 9 + 3 = 13
= = =
8 9 3
Edit: the problem is not NP-hard. The algorithm in David Eisenstat's answer is provably correct for finding a solution. However, I'll leave this answer here since it gives a way to find all solutions, which might be of interest to some.
For what it's worth, my "method of choice" is constraint programming; it's easy to model this as a constraint satisfaction problem, and then a wide range of well-developed algorithms can be applied. The code below is in Python, using the python-constraint library.
x_sums = [7, 13]
y_sums = [8, 9, 3]
from constraint import *
problem = Problem()
x_n, y_n = len(x_sums), len(y_sums)
max_num = max(x_sums + y_sums)
problem.addVariables(range(x_n * y_n), range(max_num + 1))
for i, x in enumerate(x_sums):
v = [ i + x_n * j for j in range(y_n) ]
problem.addConstraint(ExactSumConstraint(x), v)
for j, y in enumerate(y_sums):
v = [ i + x_n * j for i in range(x_n) ]
problem.addConstraint(ExactSumConstraint(y), v)
solution = problem.getSolution()
for i in range(x_n):
print(*( solution[i + x_n * j] for j in range(y_n) ))
Output: it finds a different solution to yours. Alternatively, you could search for all solutions; there are 26 of them.
4 0 3
4 9 0
The time complexity of this is hard to pin down exactly; as a very weak upper bound we can say it's definitely at most O(max_num ** (x_n * y_n)) since that's the size of the search space. In practice it is much better than that, but the algorithm this library uses is rather complicated and difficult to analyse precisely. It's a backtracking search, but with some clever ways of using the constraints to eliminate the vast majority of branches from the search tree.
For some idea of how deep this rabbit hole goes, the Handbook of Constraint Programming gives a lot of details about techniques that constraint-solving algorithms can use to improve efficiency.

Unique combinations of numbers that add up to a sum

I was asked this in an interview recently and got completely stumped. I know there are questions like this asked on here before but none handled the little twist thrown onto this one.
Given a number, find all possible ways you can add up to it using only the numbers 1,2,3. So for an input of 3, the output would be 4 because the combinations would be 1,1,1 and 1,2 and 2,1 and 3. I know about the coin change algorithm but it doesn't give me that permutation of 1,2 and 2,1. So I just ended up implementing the coin change algorithm and couldn't get the permutation part. Does anybody have any ideas?
It's a recursive problem:
take for example the possible options for 5
X X X X X
1 X X X X
2 X X X
3 X X
So
f(5)=f(4) + f(3) + f(2)
So the generic solution is
f(1)=1
f(2)=2
f(3)=4
f(N)= f(N-1) + f(N-2) + f(N-3) for N > 3
To answer your question about classification of the problem it looks like dynamic programming problem to me. See following question taken from stanford.edu
1-dimensional DP Example
◮ Problem: given n, find the number of different ways to write
n as the sum of 1, 3, 4
◮ Example: for n = 5, the answer is 6
5 = 1 + 1 + 1 + 1 + 1
= 1 + 1 + 3
= 1 + 3 + 1
= 3 + 1 + 1
= 1 + 4
= 4 + 1
And here is the solution to similar problem

Number of distinct paths between a point a 2D plane and the origin of the plane [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose, I am at the origin of a 2D plane. I want to reach point(x,y) by making exactly N steps.
If I am currently at point (p,q) then, I can go to points (p+1,q), (p,q+1), (p-1,q), (p,q-1) after one step.
How many different routes can I use to do that? Note that : N will be at most 10 million .
New answer here:
After analysis of the calculated tables - it is possible to find number of paths with some combinatorics.
Delphi code (note that long arithmetics should be used for big numbers beyond Int64 range):
function PathCount(x, y, N: Integer): Int64;
var
t, Diff: Integer;
begin
x := Abs(x); //exploit symmetry
y := Abs(y);
if y > x then begin //Swap them for simplicity, exploit symmetry again
t := x;
x := y;
y := t;
end;
Diff := N - (x + y);
if (Diff < 0) or Odd(Diff) then
Exit(0); //return 0 for unavailable points
Diff := Diff div 2;
Result := CombinationCount(N, x + Diff) * CombinationCount(N, Diff);
end;
function CombinationCount(n, k: Integer): Int64;
var
i: Integer;
begin
Result := 1;
if k > n - k then
k := n - k;
for i := 1 to k do
Result := (Result * (n - i + 1)) div i;
end;
Old answer (for demonstration)
For reasonable N it is possible to use dynamic programming. Make 3d-array with limits (-N/2..N/2),(-N/2..N/2),(0..N). Remember that its size is N^3 (10^21 for 10 million points, impractical). You can exploit symmetry, but reducing factor is small constant only (2 or 4).
Recursive formula:
P(p, q, K) = P(p-1, q, K-1) + P(p+1, q, K-1) + P(p, q-1, K-1) + P(p, q+1, K-1)
Fill array layer by layer: at the first step make P(x-1,y0,1) = 1 (and 3 points more) and so on...
Neighbourhood of the inital point after 0, 1 and 2 steps:
0 0 1 0 0
0 0 0 0 1 0 0 2 0 2 0
0 1 0 1 0 1 1 0 4 0 1
0 0 0 0 1 0 0 2 0 2 0
0 0 1 0 0
6 steps animated:
After the finish, P(0, 0, N) will contain number of paths.
P.S. Probably, there is some combinatorial formula. For example, we can see binomial coefficients C(N,K) in the last diagonals (1 2 1, 1 3 3 1, 1 4 6 4 1 ...), next non-zero diagonal contains N * C(N,K) an so on.

Please explain to me the solution for the problem below [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Problem:
Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n + 1)-element array C. State the problem formally and write pseudocode for adding the two integers.
Solution:
C ← [1 ... n + 1] ▹ C is zero-filled.
for i ← 1 to n
do sum ← A[i] + B[i] + C[i]
C[i] ← sum % 2
C[i + 1] ← sum / 2 ▹ Integer division.
output C
Question:
I thought the C[i] is A[i]+B[i] why are you adding sum ← A[i] + B[i] + C[i] in step 3?
why sum % 2 (why need to use modulo in step 4?)
why sum / 2 (why need to use division in step 5?)
Could you please explain above solution with real example? Thanks.
C is both the solution and the carry. For a real example, let's add 11 + 3. I'll write in binary with decimal in parens)
A = 1011 (11) + B = 0011 (3) [C starts as 00000 (0)]
^ ^ ^
The ^s mark the first position, and we go left, since we read left to right, but math goes right to left. Also, we divide integers, so 3/2 = 1, not 1.5. Now the steps.
1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^ ^ ^ ^ ^
i A B C, all at position i note that we store the carry for the NEXT step
Result: C = 01110 (14)
You add C[i] as well because C[i] may contain a carry bit from when you added A[i-1] + B[i-1] + C[i-1] in the previous iteration. For example if we do 1 + 1, our first iteration sum = 1 + 1 + 0 = 2, but since this is binary we have to carry the 1 and put it on C[1] and put the remainder (2 % 2 = 0) in C[0]. This gives us 10
C[i] gets sum % 2 because the sum of A[i] + B[i] + C[i] could be more than 1, but 1 is the most that will fit in that digit. The rest of the sum (if there is any) will be put in the carry bit. And that brings us to...
C[i+1] gets assigned sum / 2 because sum / 2 is the carry amount. It will be used in the next iteration when we do A[i] + B[i] + C[i] for i = i + 1.
You can think of adding binary numbers the same way you add base 10 numbers: there is an "add" step and a "carry" step to perform at each digit.
So, let's take the math one bit at a time. Say we're adding:
101
+
011
For the first step, we start on the far-right. (In your example, this corresponds to i=1). We add (1+1)%2, which gives us 0. What's really going on here? 1+1 is 2, which in binary is a two-digit number ("10"). We can only write the lower-order digit ("0"), so expressing the sum "mod 2" is really just saying "don't worry about the carry-over sum for now." So we've got:
101
+
011
---
0 (carrying a 1)
Now we implement the "carry a 1" by doing integer division ("sum / 2"), and temporarily storing it:
101
+
011
---
10
Now we are ready to add the 2nd digits: (0+1)%2 -- but we must add in the carry-over 1 that we've been keeping track of, so we take (0+1+1)%2 yielding:
101
+
011
---
00
Again we need to keep track of carry bit, giving us (0+1+1)=1:
101
+
011
---
100
Finally we add the 3rd bits: (1+0+1)%2 to give the answer:
101
+
011
---
1000

Resources