Back to General discussions forum
Hello, I can't figure out, why my code doesn't work with the problem's input, because with smaller inputs it seems to work just fine. I am using Java, and this is my code is in comments
nvm, I simply had to use BigInteger
Well I don't see your code.
But still I can give you a hint, you can divide the numbers after each operation and use the remainder for next operation. This will keep the result from overflowing to the negative side.
first find term and all values are %term by using array int term = y[stepcount-1]; for(int i = 0; i < stepcount; i++) { if(op[i] == '*'){x = x * y[i] % term;} if(op[i] == '+'){x = (x + y[i]) % term;} } x = x % term;
nvm, I simply had to use BigInteger
that's not correct :)
BigInteger will work here, but fail in further similar problems, due to long calculation. The same with default
endless int
in Python.
Look at this problem for example.
You had to read the "Modular Arithmetic" article referenced in the problem statement instead - then your soultion would be similar to what colleagues explained above - and without heavy calculations.