I am trying to solve the problem Secret Code on SPOJ, and it's obviously a math problem.
The full problem
For those who are lazy to go and read, it's like this:
a0, a1, a2, ..., an - sequence of N numbers
B - a Complex Number (has both real and imaginary components)
X = a0 + a1*B + a2*(B^2) + a3*(B^3) + ... + an*(B^n)
So if you are given B and X, you should find a0, a1, ..an.
I don't know how or where to start, because not even N is known, just X and B.
The problem is not as easy as expressing a number in a base B, because B is a complex number.
How can it be solved?
The key is that a0 .. an are not arbitrary numbers, they're integers (otherwise, this wouldn't be possible in general). You're given the number X , and are asked to express it in base B. Why don't you start by working a few examples for a specific value of B?
If I asked you to write 17 in base 2, would you be able to do that? Can you find a way to generalize the algorithm you use to bases other than 2?
Related
I am trying to write a piece of code for my old Highschool teacher for a game he had us play literally called the "Dice Game." Let's just say that the game takes two d12's and multiplies them together to get a number (D) in this instance. Then you take 3 d6's and roll them to get your A, B, and C variables. You would then either Add, Subtract, Multiply, Divide, Exponentiate, or Root by that number to get as close to as you could to D. Those operations would stand for x and y in the following equation
AxByC=D
I don't know how else to word this, but I am having trouble finding any way to solve these equations and then compare them. Maybe I am missing something simple, but I don't know.
EDIT: I should probably be more clear about the question. I know how to set all the equations up. It is just a matter of finding a way to compare the answers to the D variable and then the other answers to the equation to see which one is closer. The closest number to D wins, thus the whole point to the dice game.
If you are just trying to compare the answers to the D variable, why not loop through each equations result and compare them equal to D?
for (int i = 0; i < equationResults.size(); i++) {
if (equationResults[i] == D)
return true;
}
EDIT: If you are trying to find the closest to D, you can compare each answer to D and subtract the answer from D and store it, then return the min value:
closeToD[0] = D - equationResults[0];
return *min_element(closeToD.begin(), closeToD.end());
Since you can juggle the values around, as well as picking operators, you actually have two problems: generating the permutations of variables and generating the permutations of operators. The first part is rather straightforward:
std::array<int, 3> input;
std::sort(input.begin(), input.end());
do {
compute(input[0], input[1], input[2]);
} while (std::next_permutation(input.begin(), input.end()));
The compute part could be a function that takes such an array of 3 values and finds the best value, or closest to D, or just all values.
Generating all permutations of operators is slightly more annoying because next_permutation can't compare them, and also we accept duplicates. The easiest way is to just brute-force through them; I'll do it just for the slightly easier operators:
std::array<int, 16> compute(int a, int b, int c) {
return {
a + b + c,
a + b - c,
a + b * c,
a + b / c,
a - b + c,
a - b - c,
a - b * c,
a - b / c,
a * b + c,
a * b - c,
a * b * c,
a * b / c,
a / b + c,
a / b - c,
a / b * c,
a / b / c,
};
}
Generating such list of operations programmatically is a bit more challenging; you can't simply do (a op b) op c because of the aforementioned precedence. Doing it this way guarantees that the results are actually achievable because of the operator precedence built into the language.
This will still do redundant computations - e.g. in the first case, the result will be the same regardless of the permutation of a/b/c. Eliminating those is perhaps a more interesting exercise for later. Perhaps a small relief is the fact that if a == b or b == c, next_permutation will already take care of that for us, cutting the number of iterations from 6 to either 3 or 1.
I've a pool of numbers and from these one number X has been XORed with all the others. From these comparisons the minimum XOR values are stored in a list in a sorted format.
How can I retrieve X ?
e.g.
List: { 3, 2, 1, 0, 15, 14, 13, 12 }
Looking for X so that:
X ^ 3 < X ^ 2 < X ^ 1 <... < X ^ 12
Might not be only one X or even none X. Is there any way to revert the process of a XOR when we don't know the initial value and the result of it but just it's comparative values? How can this be solved efficiently given we know the whole pool of numbers?
You probably can't.
XOR is a really special operation. Its traits makes it impossible to figure out anything given only one of the operands, or the result.
If A xor B == C, then we have all the other five expressions:
C xor A == B
B xor C == A
C xor B == A
A xor C == B
B xor A == C
If you see this, you should know it's impossible to get one value from another. Two is always needed for the third one.
It depends very much on the outputs you have. The problem might be underconstrained, giving many possible X values. Or it might be impossible (no X satisfies the constraints).
One approach would be to pair values that differ only in the most-significant bit. If the smaller of each pair appears before the larger, the MSB must be 0; if the larger appears first, then the MSB must be 1.
With that knowledge, we can consider pairs that differ only in the first two bits, and so on.
If certain conditions were true in the output, the value of X could be determined fairly efficiently.
For example:
If the value 15 were in the output array, then you would know that X is a binary complement of one of the other numbers. Also, if the value 0 were in the output array, you would know that this was the result of X being XORed with itself.
A general algorithm with an efficient time complexity would be difficult, however.
The question is very simple and direct, but I could not find an answer on the documentation. If I have
mpfr_t a, b;
Can I do something like
mpfr_add(a, a, b, rnd);
This would compute the sum of a and b and store the result on a. I don't know if this results in an aliasing problem, potentially invalidating the result, or if this is fine.
Never mind, it's in section 4.3 of the linked document.
MPFR allows you to use the same variable for both input and output in the same expression. For example, the main function for floating-point multiplication, mpfr_mul, can be used like this: mpfr_mul (x, x, x, rnd). This computes the square of x with rounding mode rnd and puts the result back in x.
I want big int in SML. please let me know direction.
I can make normal fibonacci. but I have to print fibo(100) only using int not intinf.
fun fibo 0 = 0
| fibo n =
if n <= 2
then 1
else fibo (n-1) + fibo(n-2)
I have to print fibo(100)
only using int, not IntInf.int.
To address each of these,
Just printing (and not storing) fibo(100) does not necessarily involve finding a good representation for big numbers, so I would change this goal into "I have to find a way to represent big numbers so I can add them and display them."
But, as John points out, overflowing int values is not your only concern when calculating fibo(100). Just try your naive implementation for fibo(40) or so. It doesn't overflow, but it takes at least a few seconds on my computer. Now try fibo(41), fibo(42), etc. and witness the exponential growth. Your algorithm not only needs a representation of big integers, it also needs a sub-exponential resource usage.
E.g. by making the function tail-recursive:
fun fib n =
let fun fib_ a b 0 = a (* or b *)
| fib_ a b i = fib_ b (a+b) (i-1)
in fib_ (Int.toLarge 0) (Int.toLarge 1) n end
You're essentially asking how to represent numbers that are bigger than ints using ints only, which, logically, is not possible. But perhaps you mean "by inventing a representation that uses multiple ints in series." That's exactly what IntInf.int does. See for example How to use IntInf or LargeInt in SML?.
A naive imlementation of big integers could keep lists of ints, add them pairwise and carry over when handle Overflow is triggered. Or simply strings of digit characters! But it sounds more like a mental exercise than something useful.
I want to make structure storing value with two keys - 'x' and 'y'. I thought i will use std::map>. But I think it would be better to use just one std::map and combine both keys into one key (I will call it 'p').
So I need function p->(x,y) and function (x,y)->p. Both 'x' and 'y' are integer types with negative values allowed.
I think that it should be possible but I am surprised that I did not find tons of articles about it on the internet.
I have some ideas about how to do it, but all seems too complicated. Could you please give me some advices about how to do it or what topics to read about this problematics?
Example of possible mapping (but I need mathematical form of something like that): http://i.stack.imgur.com/UbVaM.png
Thank you for any advices :-)
I would just use a std::map<std::tuple<int,int>, int> for example, where your tuple is (x,y) and the value is z. Otherwise it will be difficult to keep a 1-to-1 mapping of p(x,y) to z.
How about using union?
union p_t {
p_t(int64_t c) : k(c) {}
p_t(int32_t a, int32_t b) : x(a), y(b) {}
int64_t k;
struct { int32_t x, y; };
};
std::map<decltype(p_t::k), int> m1;
std::unordered_map<decltype(p_t::k), int> m2;
p_t p = p_t(rand(), rand());
m1[p.k] = rand();
m2[p.k] = rand();
Live example: http://ideone.com/jebyG6
You could try and use functions similar to that used to show there are as many fractions as whole numbers This gives a unique integer for each fraction and a unique fraction for each positive integer. It works just as well for x, y coordinates. The two functions p->(x,y) and (x,y)->p are a little complicated but possible to write. You could adapt the idea using some sort of spiral shape.
.
Image from https://commons.wikimedia.org/wiki/File:Diagonal_argument.svg