I was solving a coding question, and found out the following relation to find the number of possible arrangements:
one[1] = two[1] = three[1] = 1
one[i] = two[i-1] + three[i-1]
two[i] = one[i-1] + three[i-1]
three[i] = one[i-1] + two[i-1] + three[i-1]
I could have easily used a for loop to find out the values of the individual arrays till n, but the value of n is of the order 10^9, and I won't be able to iterate from 1 to such a huge number.
For every value of n, I need to output the value of (one[n] + two[n] + three[n]) % 10^9+7 in O(1) time.
Some results:
For n = 1, result = 3
For n = 2, result = 7
For n = 3, result = 17
For n = 4, result = 41
I was not able to find out a general formula for n for the above after spending hours on it. Can someone help me out.
Edit:
n = 1, result(1) = 3
n = 2, result(2) = 7
n = 3, result(3) = result(2)*2 + result(1) = 17
n = 4, result(4) = result(3)*2 + result(2) = 41
So, result(n) = result(n-1)*2 + result(n-2) OR
T(n) = 2T(n-1) + T(n-2)
You can use a matrix to represent the recurrence relation. (I've renamed one, two, three to a, b, c).
(a[n+1]) = ( 0 1 1 ) (a[n])
(b[n+1]) ( 1 0 1 ) (b[n])
(c[n+1]) ( 1 1 1 ) (c[n])
With this representation, it's feasible to compute values for large n, by matrix exponentation (modulo your large number), using exponentation by squaring. That'll give you the result in O(log n) time.
(a[n]) = ( 0 1 1 )^(n-1) (1)
(b[n]) ( 1 0 1 ) (1)
(c[n]) ( 1 1 1 ) (1)
Here's some Python that implements this all from scratch:
# compute a*b mod K where a and b are square matrices of the same size
def mmul(a, b, K):
n = len(a)
return [
[sum(a[i][k] * b[k][j] for k in xrange(n)) % K for j in xrange(n)]
for i in xrange(n)]
# compute a^n mod K where a is a square matrix
def mpow(a, n, K):
if n == 0: return [[i == j for i in xrange(len(a))] for j in xrange(len(a))]
if n % 2: return mmul(mpow(a, n-1, K), a, K)
a2 = mpow(a, n//2, K)
return mmul(a2, a2, K)
M = [[0, 1, 1], [1, 0, 1], [1, 1, 1]]
def f(n):
K = 10**9+7
return sum(sum(a) for a in mpow(M, n-1, K)) % K
print f(1), f(2), f(3), f(4)
print f(10 ** 9)
Output:
3 7 17 41
999999966
It runs effectively instantly, even for the n=10**9 case.
Related
B is a subsequence of A if and only if we can turn A to B by removing zero or more element(s).
A = [1,2,3,4]
B = [1,4] is a subsequence of A.(Just remove 2 and 4).
B = [4,1] is not a subsequence of A.
Count all subsequences of A that satisfy this condition : A[i]%i = 0
Note that i starts from 1 not 0.
Example :
Input :
5
2 2 1 22 14
Output:
13
All of these 13 subsequences satisfy B[i]%i = 0 condition.
{2},{2,2},{2,22},{2,14},{2},{2,22},{2,14},{1},{1,22},{1,14},{22},{22,14},{14}
My attempt :
The only solution that I could came up with has O(n^2) complexity.
Assuming the maximum element in A is C, the following is an algorithm with time complexity O(n * sqrt(C)):
For every element x in A, find all divisors of x.
For every i from 1 to n, find every j such that A[j] is a multiple of i, using the result of step 1.
For every i from 1 to n and j such that A[j] is a multiple of i (using the result of step 2), find the number of B that has i elements and the last element is A[j] (dynamic programming).
def find_factors(x):
"""Returns all factors of x"""
for i in range(1, int(x ** 0.5) + 1):
if x % i == 0:
yield i
if i != x // i:
yield x // i
def solve(a):
"""Returns the answer for a"""
n = len(a)
# b[i] contains every j such that a[j] is a multiple of i+1.
b = [[] for i in range(n)]
for i, x in enumerate(a):
for factor in find_factors(x):
if factor <= n:
b[factor - 1].append(i)
# There are dp[i][j] sub arrays of A of length (i+1) ending at b[i][j]
dp = [[] for i in range(n)]
dp[0] = [1] * n
for i in range(1, n):
k = x = 0
for j in b[i]:
while k < len(b[i - 1]) and b[i - 1][k] < j:
x += dp[i - 1][k]
k += 1
dp[i].append(x)
return sum(sum(dpi) for dpi in dp)
For every divisor d of A[i], where d is greater than 1 and at most i+1, A[i] can be the dth element of the number of subsequences already counted for d-1.
JavaScript code:
function getDivisors(n, max){
let m = 1;
const left = [];
const right = [];
while (m*m <= n && m <= max){
if (n % m == 0){
left.push(m);
const l = n / m;
if (l != m && l <= max)
right.push(l);
}
m += 1;
}
return right.concat(left.reverse());
}
function f(A){
const dp = [1, ...new Array(A.length).fill(0)];
let result = 0;
for (let i=0; i<A.length; i++){
for (d of getDivisors(A[i], i+1)){
result += dp[d-1];
dp[d] += dp[d-1];
}
}
return result;
}
var A = [2, 2, 1, 22, 14];
console.log(JSON.stringify(A));
console.log(f(A));
I believe that for the general case we can't provably find an algorithm with complexity less than O(n^2).
First, an intuitive explanation:
Let's indicate the elements of the array by a1, a2, a3, ..., a_n.
If the element a1 appears in a subarray, it must be element no. 1.
If the element a2 appears in a subarray, it can be element no. 1 or 2.
If the element a3 appears in a subarray, it can be element no. 1, 2 or 3.
...
If the element a_n appears in a subarray, it can be element no. 1, 2, 3, ..., n.
So to take all the possibilities into account, we have to perform the following tests:
Check if a1 is divisible by 1 (trivial, of course)
Check if a2 is divisible by 1 or 2
Check if a3 is divisible by 1, 2 or 3
...
Check if a_n is divisible by 1, 2, 3, ..., n
All in all we have to perform 1+ 2 + 3 + ... + n = n(n - 1) / 2 tests, which gives a complexity of O(n^2).
Note that the above is somewhat inaccurate, because not all the tests are strictly necessary. For example, if a_i is divisible by 2 and 3 then it must be divisible by 6. Nevertheless, I think this gives a good intuition.
Now for a more formal argument:
Define an array like so:
a1 = 1
a2 = 1× 2
a3 = 1× 2 × 3
...
a_n = 1 × 2 × 3 × ... × n
By the definition, every subarray is valid.
Now let (m, p) be such that m <= n and p <= n and change a_mtoa_m / p`. We can now choose one of two paths:
If we restrict p to be prime, then each tuple (m, p) represents a mandatory test, because the corresponding change in the value of a_m changes the number of valid subarrays. But that requires prime factorization of each number between 1 and n. By the known methods, I don't think we can get here a complexity less than O(n^2).
If we omit the above restriction, then we clearly perform n(n - 1) / 2 tests, which gives a complexity of O(n^2).
Problem
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
Suggested Solution (BFS)
def numSquares(self, n):
if n < 2:
return n
lst = []
i = 1
while i * i <= n:
lst.append( i * i )
i += 1
cnt = 0
toCheck = {n}
while toCheck:
cnt += 1
temp = set()
for x in toCheck:
for y in lst:
if x == y:
return cnt
if x < y:
break
temp.add(x-y)
toCheck = temp
return cnt
How does this particular BFS run in O(sqrt(n))? Because what I am thinking is finding squares take O(sqrt(n)). Because there are 2 for loops, (for y in lst1 takes O(sqrt(n)), for x in toCheck takes O(sqrt(n)), shouldn't it be O(n)??
The running time is actually Theta(n^(3/2)). According to Legendre's three-square theorem, any integer of the form 4^a (8b + 7) for integers a and b can be written as the sum of four squares but not three. Let n be an integer of this kind. There are Omega(n) numbers less than n that can be written as the sum of three squares, so in the final iteration of the while loop, toCheck has Theta(n) elements, and lst has Theta(n^(1/2)).
Given an array of integers A and integers N, M. I want to find all the subsets S of A where (sum(S) mod M = N).
A can have multiple integers of the same value.
In my case N will be in the range 0<=n<=31, M will be 32 and A will contain integers in the same range as n.
Is there any good/"fast" way to do this?
Thanks!
It is solvable in O(2n/2 log2(2n/2)) = O(2n/2 (n/2)), with your constrains this works on C++ less than a second.
All you need is:
1) compute all possible sums of first n/2 elements of the array and put them in map<int, int> left where left[sum] = how many times sum appears at the left part of the array
2) compute all possible sums of last n/2 elements of the array and for each sum S check does map left contains value (N - S + M)%M
to find all possible sums you could use bitmasks:
for (int mask = 1; mask < pow(2, n/2); mask++) {
int sum = 0;
for (int i = 0; i < n/2; i++)
if ( (int) (mask & (1<<i)) )
sum += A[i];
}
If you'd just like to count them, we can solve it in O(|A| * M) with dynamic programming. Here's an example:
A = [2, 6, 4, 3]
M = 5
0 1 2 3 4
S = 0 0 0 0 0 // The number of subsets with sum i (mod M)
// Iterate over A (through S each time)
2 0 0 1 0 0
6 0 1 1 1 0
4 1 2 2 1 1
3 3 3 3 3 3
Python code:
A = [2, 6, 4, 3]
M = 5
S = [0 for i in range(0, M)]
for a in A:
STemp = [0 for i in range(0, M)]
for (i, v) in enumerate(S):
ii = (a + i) % M
STemp[ii] = S[ii] + v
STemp[a % M] = STemp[a % M] + 1
S = STemp
print(S) # [3, 3, 3, 3, 3]
An array of length n is given. Find the sum of products of elements of the sub-array.
Explanation
Array A = [2, 3, 4] of length 3.
Sub-array of length 2 = [2,3], [3,4], [2,4]
Product of elements in [2, 3] = 6
Product of elements in [3, 4] = 12
Product of elements in [2, 4] = 8
Sum for subarray of length 2 = 6+12+8 = 26
Similarly, for length 3, Sum = 24
As, products can be larger for higher lengths of sub-arrays calculate in modulo 1000000007.
What is an efficient way for finding these sums for subarrays of all possible lengths, i.e., 1, 2, 3, ......, n where n is the length of the array.
There is rather simple way:
Construct product of terms (1 + A[i] * x):
P = (1 + A[0] * x) * (1 + A[1] * x) * (1 + A[2] * x)...*(1 + A[n-1] * x)
If we open the brackets, then we'll get polynomial
P = 1 + B[1] * x + B[2] * x^2 + ... + B[n] * x^n
Kth coefficient, B[k], is equal to the sum of products of sets with length K - for example, B[n] = A[0]*A[1]*A[2]*..A[n-1], B[2] = A[0]*A[1] + A[0]*A[2] + ... + A[n-2]*A[n-1] and so on.
So to find sum of products of all possible sets, we have to find value of polynomial P for x = 1, then subtract 1 to remove leading 0th term. If we don't want to take into consideration single-element sets, then subtract B1 = sum of A[i].
Example:
(1+2)(1+3)(1+4) = 60
60 - 1 = 59
59 - (2 + 3 + 4) = 50 = 24 + 26 - as your example shows
We first create a recursive relation. Let f(n, k) be the sum of all products of sub-arrays of length k from an array a of length n. The base cases are simple:
f(0, k) = 0 for all k
f(n, 0) = 1 for all n
The second rule might seem a little counter-intuitive, but 1 is the zero-element of multiplication.
Now we find a recursive relation for f(n+1, k). We want the product of all subarrays of size k. There are two types of subarrays here: the ones including a[n+1] and the ones not including a[n+1]. The sum of the ones not including a[n+1] is exactly f(n, k). The ones including a[n+1] are exactly all subarrays of length k-1 with a[n+1] added, so their summed product is a[n+1] * f(n, k-1).
This completes our recurrence relation:
f(n, k) = 0 if n = 0
= 1 if k = 0
= f(n-1, k) + a[n] * f(n-1, k-1) otherwise
You can use a neat trick to use very limited memory for your dynamic programming, because function f only depends on two earlier values:
int[] compute(int[] a) {
int N = a.length;
int[] f = int[N];
f[0] = 1;
for (int n = 1; n < N; n++) {
for (int k = n; k >= 1; k--) {
f[k] = (f[k] + a[n] * f[k-1]) % 1000000007;
}
}
return f;
}
In my project, one part of problem is there. But to simplify, here the problem is being formulated. There are two positive co-prime integers: a and b, where a < b. Multiples of a from 1 through b-1 is listed followed by modulus operation by b.
a mod b , 2*a mod b , 3*a mod b, ... , (b-1)*a mod b
Now, there is another integer, say n ( 1 <= n < b). Through the first n numbers in the list, we have to find how many numbers is less than, say m (1 <= m < b). This can be done in brute force approach, thereby giving a O(n).
An example:
a=6, b=13, n=8, m=6
List is:
6, 12, 5, 11, 4, 10, 3, 9, 2, 8, 1, 7
This is a permutation of the numbers from 1 to 12 because modulus operation of any two co-primes produces a permutation of numbers if we include another number, that is, 0. If we take a= 2, b=13, then the list would have been 2, 4, 6, 8, 10, 12, 1, 3, 5, 7, 9, 11, which gives a pattern. Whereas if a and b are very large (in my project they can go up to 10^20), then I have no idea how to deduce a pattern of such large numbers.
Now getting back to the example, we take the first n = 8 numbers from the list, which gives
6, 12, 5, 11, 4, 10, 3, 9
Applying the less-than operator with m = 6, it gives the total number of numbers less than m being 3 as explained below in the list
0, 0, 1, 0, 1, 0, 1, 0
where 0 refers to not being less than m and 1 refers to being less than m.
Since, the algorithm above is a O(n), which is not acceptable for the range of [0, 10^20], so can the community give a hint/clue/tip to enable me to reach a O(log n ) solution, or even better O(1) solution?
(Warning: I got a little twitchy about the range of multipliers not being [0, n), so I adjusted it. It's easy enough to compensate.)
I'm going to sketch, with tested Python code, an implementation that runs in time O(log max {a, b}). First, here's some utility functions and a naive implementation.
from fractions import gcd
from random import randrange
def coprime(a, b):
return gcd(a, b) == 1
def floordiv(a, b):
return a // b
def ceildiv(a, b):
return floordiv(a + b - 1, b)
def count1(a, b, n, m):
assert 1 <= a < b
assert coprime(a, b)
assert 0 <= n < b + 1
assert 0 <= m < b + 1
return sum(k * a % b < m for k in range(n))
Now, how can we speed this up? The first improvement is to partition the multipliers into disjoint ranges such that, within a range, the corresponding multiples of a are between two multiples of b. Knowing the lowest and highest values, we can count via a ceiling division the number of multiples less than m.
def count2(a, b, n, m):
assert 1 <= a < b
assert coprime(a, b)
assert 0 <= n < b + 1
assert 0 <= m < b + 1
count = 0
first = 0
while 0 < n:
count += min(ceildiv(m - first, a), n)
k = ceildiv(b - first, a)
n -= k
first = first + k * a - b
return count
This isn't fast enough. The second improvement is to replace most of the while loop with a recursive call. In the code below, j is the number of iterations that are "complete" in the sense that there is a wraparound. term3 accounts for the remaining iteration, using logic that resembles count2.
Each of the complete iterations contributes floor(m / a) or floor(m / a) + 1 residues under the threshold m. Whether we get the + 1 depends on what first is for that iteration. first starts at 0 and changes by a - (b % a) modulo a on each iteration through the while loop. We get the + 1 whenever it's under some threshold, and this count is computable via a recursive call.
def count3(a, b, n, m):
assert 1 <= a < b
assert coprime(a, b)
assert 0 <= n < b + 1
assert 0 <= m < b + 1
if 1 == a:
return min(n, m)
j = floordiv(n * a, b)
term1 = j * floordiv(m, a)
term2 = count3(a - b % a, a, j, m % a)
last = n * a % b
first = last % a
term3 = min(ceildiv(m - first, a), (last - first) // a)
return term1 + term2 + term3
The running time can be analyzed analogously to the Euclidean GCD algorithm.
Here's some test code to prove evidence for my claims of correctness. Remember to delete the assertions before testing performance.
def test(p, f1, f2):
assert 3 <= p
for t in range(100):
while True:
b = randrange(2, p)
a = randrange(1, b)
if coprime(a, b):
break
for n in range(b + 1):
for m in range(b + 1):
args = (a, b, n, m)
print(args)
assert f1(*args) == f2(*args)
if __name__ == '__main__':
test(25, count1, count2)
test(25, count1, count3)