following Problem, when having to make a fast code: I have a list of 2 integers a_i and b_i and I have to compute the equation: y = (a_i * x + b_i), where I'm only interested in y, not in x. All a_i's are prime and different from each other. a_i = y / x, b_i = y % x.
There are multiple solutions for y, even if y has to be the same for all a_i, b_i pairs, as x can be any integer number. Therefore we have an upper limit k and y <= k. I want to have the highest y possible (if there is a solution). max(y).
In short: I want to know the highest integer y (if there exists one), with y <= k and the equation given. x >= 1.
I already have a "solution" that works in theory, but that is too slow:
struct part {
unsigned int size, rest;
};
int solve(vector<part> &partions, int minK, int stepSize, int k) {
int sol = 0;
for (int i = k; i >= minK; i -= stepSize) {
bool works = true;
for (int j = 0; j < static_cast<int>(partions.size()); j++) {
if ((i - partions[j].rest) % partions[j].size != 0) {
works = false;
break;
}
}
if (works) return i;
}
return -1;
}
Explanation: minK is max(a_i + b_i), as I thought that y = a_i *1 + b_i is the smallest possible solution for one equation, and as y is the same for all equations, the maximum one is the best lower bound.
stepSize is not 1 (in order to make the program faster), but max(a_i), as I thought, that as max(a_i) has to fit in y and y = a_i * x + b_i, therefore as x are integer values, the stepSize is a_i, and max(a_i) the best one, because that decreases the number of loop runs.
I now try out all y's from k to minK with stepSize and test for all pairs a_i and b_i, if they fulfill the equation. If one of them fails, I have to go on until I either find a solution or none (-1).
Unfortunately that algorithm is too slow (as a_i and b_i can go up to 10^12) and I can't think of any more optimizations. I already searched the internet for number theory and arithmetic's, but couldn't find any similar thing.
Can you help me to make this code faster or hint me to some sites, where this problem is handled?