Hash and Modular Arthmetic - c++

Let h(y) be the function defined as (a*y+b)mod m. So h(y) can take values from 0 to m-1.
Now we are given 7 integers- a,b,x,n,c,d,m. Our task is to find the total count of h(x),h(x+1),h(x+2)...h(x+n) such that the value of h(x+i) falls in the range of [c,d].where 0<=i<=n
Integer limits are:
1 ≤ m ≤ 10^15, c ≤ d < m, a,b < m, x+n ≤ 10^15, and a*(x+n) + b ≤ 10^15
For Example.
for input set {1,0,0,8,0,8,9} the output should be 9. Please suggest an efficient algorithm. Thanks!!!

This isn't a particularly strong hash. The only hard part about this problem is the obtuse notation with single-letter variables and specifying the problem as a 7-tuple.
Each increment of x increases h(x) by a. Therefore the total distance along x to get from c to d is simply (d-c)/a. Add one for the fencepost problem, or specify the problem with a half-open range for the sake of sanity.

Related

Given two segments in 3D, compute closest point between them using CGAL

Given two segments in 3D in CGAL, I would like to compute the closest points between one segment and another. These two segment may be anywhere in space.
I have looked in CGAL and there is a function which computes distance between both segments (https://doc.cgal.org/latest/Kernel_23/group__squared__distance__grp.html), and with that I guess I could create two spheres and compute the intersection between them, but this seems slow and cumbersome.
Is there something out of the box?
Sorry to say, there are no out-of-the-box way in the CGAL library to see two points, defining the shortest segment between two line segments in 3D. I'm saying "to see" because these two points are actually computed deeply inside the CGAL::squared_distance function, then the distance between them is calculated, and then these two points are discarded.
If you really need these two points you can modify the Distance_3/Segment_3_Segment_3.h file and add a function with an additional argument:
template <class K>
inline
typename K::FT
squared_distance(const Segment_3<K>& seg1,
const Segment_3<K>& seg2,
Segment_3<K>& res)
{
// ... modifications
}
Of course you'll need to modify other code in this file as well.
Analytically:
You want to minimize the function
(A + p AB - C - q CD)²
under inequality constraints 0 ≤ p ≤ 1 and 0 ≤ q ≤ 1.
By differentiation, you get a linear system of 2 equations in the 2 unknowns p and q. After resolution, if these values fall in the allowed range, you are done.
p AB² - q AB.CD = AB.AC
p CD.AB - q CD² = CD.AC
Otherwise, you need to saturate one constraint (p=0, p=1, q=0 or q=1) and check if the other parameter is in range. (Endpoint to segment distances.)
Otherwise, you saturate both constraints (4 combinations). (Endpoint to endpoint distances.)
Finally, you keep the feasible solution that yields the smallest distance.
If the two segments are parallel or collinear, there can be an infinity of solution points.
Addendum:
WLOG, AB.CD ≤ 0 (otherwise swap two endpoints). Then then from the inequalities we draw
- AB.CD ≤ p AB² - q AB.CD ≤ AB²
- AB.CD ≤ p AB.CD - q CD² ≤ CD²
which directly gives us the condition for existence of a segment-segment solution:
- AB.CD ≤ AB.AC ≤ AB²
- AB.CD ≤ CD.AC ≤ CD²
We get the conditions for endpoint-segment solutions by substituting 0 or 1 for p or q.

How to solve spoj Dquery with one more constraint

I have solved this spoj DQUERY
problem using MO's Algorithms in time complexity of N*Sqrt(N). But, now I want one more constraint on each query, that is,now each query contains the four integers i,j,x,ysuch as:
(1 ≤ i ≤ j ≤ n)
(1 ≤ x ≤ y ≤ 10^6)
For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj which is greater or equal to x and less or equal to y.
For example:
If n=5 arrays element be
1 1 2 1 3
then, for query (i,j,x,y):
1 5 2 3
answer will be:
2
because, in range from i=1 to j=5 three distinct element{1,2,3} is present but only two element{2,3} is present in range x=2 to y=3.So anwer is 2.
I am getting solution in time complexity of (Q*Sqrt(N)X), please help how to solve this question with efficient time complexity less than (QSqrt(N)*X).

Can I check whether the given number can be the sum of any arithmetic progression having n terms in it?

Is it possible for a given number s to just check that Is there any possible arithmetic progression having n terms and sum of these n terms results in s.
where starting element and difference of AP must not be zero.
for eg:
s = 24 & n = 4
yes, it is possible where AP is 3 5 7 9.
Note: I just want to check whether it is possible or not . No need to find the actual array. 0 < n < 10^9 & 0 < s < 10^18.
My Attempt:
we know that sum of an AP is equal to s = n(first+last)/2;
therefore first+last = 2*s/n;
2*s/n should be an integer.
we also know that last = first+(n-1)diff;
so my expression becomes 2*first + (n-1)diff = 2*s/n;
first = (2*s/n - (n-1)diff)/2; and it should be an integer for a particular value of diff.
this is my approach to doing this but its time complexity is too large to cover 10^18.
Please help. :)
Case 1: a and d are real numbers
Using s for the sum, n for the number of terms, a for the first term and d for the difference between terms, you get the result
2 * s / n = 2 * a + (n - 1) * d
This gives you one degree of freedom. So you can see that it's always possible to pick an infinite set of a and d values that satisfies this result.
Case 2: a and d are integer numbers
You can see from my result that if a and d are constrained to be integers, then the decomposition is only possible if the left hand side of this equation is also an integer; that is 2 * s is a multiple of n. (In your case, 2 * s is 48 which is a multiple of 4. So yes, there exists an integral a and d in that case).
Let a be the initial term of the progression and d its common difference. You want to solve the linear diophantine equation
n * a + (n*(n-1)/2) * d = s
The solution will exist if and only if s is a multiple ofgcd(n, n*(n-1)/2).
If n is odd, gcd(n, n*(n-1)/2) = n * gcd(1, (n-1)/2) = n.
If n is even, gcd(n, n*(n-1)/2) = (n/2) * gcd(2, n-1) = n/2.
In any case, the solution exists if and only if 2 * s is a multiple of n.
I think this is not possible in every case but if you can provide some more data then it can.
because there is multiple possibilities of same AP sum.
so in case you will give some hint it is possible

Integer Linear Programming formulation for Test Cover?

The Test Cover problem can be defined as follows:
Suppose we have a set of n diseases and a set of m tests we can perform to check for symptoms. We also are given the following:
an nxn matrix A where A[i][j] is a binary value representing the result of running the jth test on a patient with the the ith disease (1 indicates a positive result, 0 indicates negative);
the cost of running test j, c_j; and that
any patient will have exactly one disease
The task is to find a set of tests that can uniquely identify each of the the n diseases at minimal cost.
This problem can be formulated as an Integer Linear Program, where we want to minimize the objective function \sum_{j=1}^{m} c_j x_j, where x_j = 1 if we choose to include test j in our set, and 0 otherwise.
My question is:
What is the set of linear constraints for this problem?
Incidentally, I believe this problem is NP-hard (as is Integer Linear Programming in general).
Well if I am correct you just need to ensure
\sum_j x_j.A_ij >= 1 forall i
Let T be the matrix that results from deleting the jth column of A for all j such that x_j = 0.
Then choosing a set of tests that can uniquely distinguish any two diseases is equivalent to ensuring that every row of T is unique.
Observe that two rows k and l are identical if and only if (T[k][j] XOR T[l][j]) = 0 for all j.
So, the constraints we want are
\sum_{j=1}^{m} x_j(A[k][j] XOR A[l][j]) >= 1
for all 1 <= k <= m and 1 <= l <= 1 such that k != l.
Note that the constraints above are linear, since we can just pre-compute the coefficient (A[k][j] XOR A[l][j]).

Find {E1,..En} (E1+E2+..En=N, N is given) with the following property that E1* E2*..En is Maximum

Given the number N, write a program that computes the numbers E1, E2, ...En with the following properties:
1) N = E1 + E2 + ... + En;
2) E1 * E2 * ... En is maximum.
3) E1..En, are integers. No negative values :)
How would you do that ? I have a solution based on divide et impera but i want to check if is optimal.
Example: N=10
5,5 S=10,P=25
3,2,3,2 S=10,P=36
No need for an algorithm, mathematic intuition can do it on its own:
Step 1: prove that a result set with numbers higher than 3 is at most as good as a result set with only 3's and 2's
Given any number x in your result set, one might consider whether it would be better to divide it into two numbers.
The sum should still be x.
When x is even, The maximum for t (x - t) is reached when t = x/2 , and except for the special case x = 2, then it is greater than x, and for the special case x = 4, equal to x (see note 1).
When x is odd, The maximum for t (x - t) is reached when t = (x ± 1)/2.
What does this show? Only that you should only have 3's and 2's in your final set, because otherwise it is suboptimal (or equivalent to an optimal set).
Step 2: you should have as many 3's as possible
Now, as 3² > 2³, you should have as many 3's as possible as long as the remainder is not 1.
Conclusion: for every N >= 3:
If N = 0 mod 3, then the result set is only 3's
If N = 1 mod 3, then the result set has one pair of 2's (or a 4) and the rest is 3's
If N = 2 mod 3, then the result set has one 2 and the rest is 3's
Please correct this post. The times when I was writing well-structured mathematical proofs is far away...
Note 1: (2,4) is the only pair of distinct integers such that x^y = y^x. You can prove that with:
x^y = y^x
y ln(x) = x ln(y)
ln(x)/x = ln(y) / y
and the function ln(t)/t is strictly decreasing after its global maximum, reached between 2 and 3, so if you want two distinct integers such that ln(x)/x = ln(y)/y, one of them must be lower or equal to 2. From that you can infer that only (2,4) works
This is not a complete solution, but might help.
First off note that if you fix n, and two of the terms E_i and E_j differ by more than one (for example 3 and 8), then you can do better by "equalizing" them as much as possible, i.e., if the number p = E_i + E_j is even, you do better both terms by p/2. If p is odd, you do better by replacing them with p/2 and p/2+1 (where / is integer division).
That said, then if you knew what the optimal number of terms, n, was, you'd be done: let all E_i's equal N/n and N/n+1 (again integer division), so that their sum is still N (this is now a straightforward problem).
So the question now is what is the optimal n. Suppose for the moment that you are allowed to use real numbers. Then the solution would be N/n for each term and you could write the product as (N/n)^n. If you differentiate this with respect to n and find its root you find that n should be equal to N/e (where e is the Neper number, also known as Euler's number, e = 2.71828....). Therefore, I'd look for a solution where either n = floor(N/e) or n = floor(N/e)+1, and then choose all the E_i's equal to either N/n or N/n+1, as above.
Hope that helps.
The Online Encycolpedia of Integer Sequences gives a recurrence relation for the solution to this problem.
I'll leave it up to someone else to compare complexities. Not sure I can figure out the complexity of OP's method.