modulo

How can I calculate (A*B)%C for A,B,C <= 10^18, in C++?

半腔热情 提交于 2019-12-18 06:19:49
问题 For example, A=10^17, B=10^17, C=10^18. The product A*B exceeds the limit of long long int. Also, writing ((A%C)*(B%C))%C doesn't help. 回答1: Assuming you want to stay within 64-bit integer operations, you can use binary long division, which boils down to a bunch of adds and multiply by two operations. This means you also need overflow-proof versions of those operators, but those are relatively simple. Here is some Java code that assumes A and B are already positive and less than M. If not, it

Modulus/Remainder function for non-integers

允我心安 提交于 2019-12-18 05:34:29
问题 rem gives this: Prelude> rem 9 8 1 I wanted something like this: Prelude> nonIntRem 9.1 8 1.0999999999999996 I implemented it like this: nonIntRem x y = x - (y * (fromIntegral $ truncate (x/y))) My questions are: Does something like this already exist in a standard Haskell library? I'd prefer to use a standard function, and I may have missed it. If not, is there a more standard name for this function in other languages? Maybe fmod, but the behavior for negatives in this case is not like mod,

random over a range, is number bias present for new rand() version?

旧街凉风 提交于 2019-12-18 04:26:25
问题 reading from various other SO questions, when using rand() % N you may happen to modify the bias for the pseudo number you get, so you usually have to introduce some range handling. However in all cases rand() was always mentioned, and not the newer random() or arcrandom4() functions or the native C++11 methods. What happens when you run these routines over a set? Do you get a bias like rand()? Thanks. 回答1: What happens when you run these routines over a set? Do you get a bias like rand()?

What's the idea of doing x mod 1000000007? [closed]

眉间皱痕 提交于 2019-12-17 18:26:38
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . In many programming problems (e.g. some Project Euler problems) we are asked to report the answer as the remainder left after dividing the answer by 1,000,000,007. Why not any other number? Edit: 2 years later,

Calculate a*a mod n without overflow

落花浮王杯 提交于 2019-12-17 09:56:32
问题 I need to calculate a*a mod n but a is fairly large, resulting in overflow when I square it. Doing ((a % n)*(a % n)) % n doesn't work because (n-1) 2 can overflow. This is in C++ and I'm using int64_t Edit: Example value: a = 821037907258 and n = 800000000000, which overflows if you square it. I am using DevCPP and I've already tried getting big-integer libraries working to no avail. Edit 2: No, there's no pattern to these numbers. 回答1: If you can't use a big-integer library, and you don't

Fastest way to calculate a 128-bit integer modulo a 64-bit integer

空扰寡人 提交于 2019-12-17 08:30:10
问题 I have a 128-bit unsigned integer A and a 64-bit unsigned integer B. What's the fastest way to calculate A % B - that is the (64-bit) remainder from dividing A by B? I'm looking to do this in either C or assembly language, but I need to target the 32-bit x86 platform. This unfortunately means that I cannot take advantage of compiler support for 128-bit integers, nor of the x64 architecture's ability to perform the required operation in a single instruction. Edit: Thank you for the answers so

Is there any alternative to using % (modulus) in C/C++?

一笑奈何 提交于 2019-12-17 07:12:09
问题 I read somewhere once that the modulus operator is inefficient on small embedded devices like 8 bit micro-controllers that do not have integer division instruction. Perhaps someone can confirm this but I thought the difference is 5-10 time slower than with an integer division operation. Is there another way to do this other than keeping a counter variable and manually overflowing to 0 at the mod point? const int FIZZ = 6; for(int x = 0; x < MAXCOUNT; x++) { if(!(x % FIZZ)) print("Fizz\n"); //

Best way to make Java's modulus behave like it should with negative numbers?

≯℡__Kan透↙ 提交于 2019-12-17 02:59:21
问题 In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ? b + a : a % b 回答1: It behaves as it should a % b = a - a / b * b; i.e. it's the remainder. You can do (a % b + b) % b This expression works as the result of (a % b) is necessarily lower than b , no matter if a is positive or negative. Adding b takes care of the negative values of a , since (a % b) is a

How to calculate modulus of large numbers?

浪子不回头ぞ 提交于 2019-12-17 01:39:08
问题 How to calculate modulus of 5^55 modulus 221 without much use of calculator? I guess there are some simple principles in number theory in cryptography to calculate such things. 回答1: Okay, so you want to calculate a^b mod m . First we'll take a naive approach and then see how we can refine it. First, reduce a mod m . That means, find a number a1 so that 0 <= a1 < m and a = a1 mod m . Then repeatedly in a loop multiply by a1 and reduce again mod m . Thus, in pseudocode: a1 = a reduced mod m p =

What is the result of % in Python?

戏子无情 提交于 2019-12-16 19:38:12
问题 What does the % in a calculation? I can't seem to work out what it does. Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How? 回答1: The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0