2

I have an array Array = {}, size of array is n

My constraints are like this:

n <= 100000 and Arrayi <=100

I have to find the products of all the elements in the array, I will be given a mod value with which I have to mod the product. The mod will value changes all the time and this mod value is always less than or equal to n.

My problem is when I chose, a global mod value say R = 1000000000 (which is far bigger than mod constraint) and whenever my product exceeds this value, I mod the result.

But I dont know why the result Im obtaining is zero.

My question is how do I chose R in such situations?

user650521
  • 583
  • 1
  • 9
  • 23
  • 2
    The reason you're getting a zero result is probably because there's a bug in your code. It might help if you actually showed us that code (preferably in the form of a [short, self-contained correct example](http://sscce.org)). – Ilmari Karonen Aug 10 '13 at 11:56
  • What are the range limits on the modulus? (Ie, on your “mod value” number) – James Waldby - jwpat7 Aug 10 '13 at 14:53

3 Answers3

1

You haven't showed us your code, but presumably it looks something like the following pseudo-Python code:

limit = 1000000

def product_mod( array, m ):
    product = 1 
    for k in array:
        product = product * k
        if product > limit: product = product % m

    return product % m

This algorithm should work, provided that the limit is low enough that product * k can never overflow. If it doesn't, you probably have a bug in your code.

However, note that it's quite likely that the result of this function may often be legitimately zero: specifically, this will happen whenever the product of the numbers in your array evenly divides the modulus. Since the product will typically be a highly composite number (it will have at least as many factors as there are numbers in the array), this is pretty likely.

In particular, the output of the function will be zero whenever:

  • any one of the numbers in the array is zero,
  • any one of the numbers in the array is a multiple of the modulus, or
  • the product of any subset of the numbers in the array is a multiple of the modulus.

In all those cases, the product of all the numbers in the array will be either zero or a multiple of the modulus (and will thus reduce to zero).

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • From the question it seems like @user650521 is doing something like `if product > limit: product = product % limit` – Terje D. Aug 10 '13 at 15:16
1

I dont know your code but it is likey that 0 is correct result.

Pick R large prime and make sure that none of elements is divisible by this number in order to get result different from 0.

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
0

It sounds that you are calculating the product of the values in the array modulo some given value, but are using another value to limit integer overflow in the intermediate calculations. Then you have a high risk of getting a wrong result.

E.g. 120 mod 9 = 3 while (120 mod 100) mod 9 = 20 mod 9 = 2

The correct procedure is then to do all the calculations modulo the same number as you are to use for the final result since (a * b) mod n = (a mod n) * (b mod n) mod n

E.g. (24 * 5) mod 9 = (24 mod 9) * (5 mod 9) mod 9 = (6 * 5) mod 9 = 30 mod 9 = 3

Terje D.
  • 6,250
  • 1
  • 22
  • 30