0

There is a variable first_variable which is always a mod of some number, mod_value. In every step first_variable is multiplied with some number second_variable.

And the range of all three variables is from 1 to 10^18.

For that I build a formula,

first_variable = ((first_variable%mod_value)*(second_variable%mod_value))%mod_value

But this gives a wrong answer,

For example, If first_variable and second_variable is (10^18)-1 and mod_value = 10^18

Please suggest me method, so that first_variable will always give right answer.

MoonBun
  • 4,322
  • 3
  • 37
  • 69
devsda
  • 4,112
  • 9
  • 50
  • 87
  • 1
    What is the programming language you used? There must be an overflow. If you are using 64 bit for smaller length fixed size variables (eg, long in java (64 bit), int in java (32 bit), this kind of multiplication overflows if the result is larger than the maximum number that data type can hold. – Buddhima Gamlath Jan 06 '14 at 10:10
  • I am using C++. IS it possible to solve by using `unsigned long long or long long` ? – devsda Jan 06 '14 at 10:11
  • Unlike your platform is something very exotic, `long long` won't help. It's only 64 bits on Windows (Visual Studio, etc.) and Unix. OTOH, GCC has __int128 type implemented in its own library. You can try it. – Netch Jan 06 '14 at 10:14
  • possible duplicate of [How can I calculate (A\*B)%C for A,B,C <= 10^18, in C++?](http://stackoverflow.com/questions/20912109/how-can-i-calculate-abc-for-a-b-c-1018-in-c) – xan Jan 13 '14 at 14:23
  • dup of http://stackoverflow.com/questions/20912109/how-can-i-calculate-abc-for-a-b-c-1018-in-c/20912380 – xan Jan 13 '14 at 14:23

3 Answers3

0

Seems you are using a runtime where arithmetic is implemented using 64-bit integers. You can check this using multipliers like 2^32: if their product is 0, my guess is true. In that case, you should switch to an arbitrary long arithmetic implementation, or at least one that is much longer than the current one. E.g. Python supports integers up to 2^1016 (256^127), same for Erlang.

I've seen in comments you use C++. If so, look for GMP library and analogs. Or, if 128 bits is enough, modern GCC support it through own library.

Netch
  • 4,171
  • 1
  • 19
  • 31
0

This is basically overflows, so you should either use different value for mod_value (up to 10^9) or limit the range for first value and second value.

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
0

Your number is O(10^36) which is O(2^108) which cannot fit in any primitive data type in languages like java or C++. Use BigInt in C++ or Java or use numpy in python to get over it.

Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
  • Big-Oh notation isn't suitable for this. O() takes functions as parameters, and if `10^36` were to stand for `f(n) = 10^36` the number would also be in O(-1) or O(0). It just doesn't make sense to use it for this, because it doesn't tell you anything. – phant0m Jan 25 '14 at 13:35
  • @phant0m I meant the numbers can be as large as 10^36 and yes O() notation is not good to use but i think it conveys the message. – Vikram Bhat Jan 25 '14 at 14:21