×

EASY

# PREREQUISITES

Simple Math, Repeated Squaring

# PROBLEM

You are given a list of N integers.

Each value in the list is between 1 and 100.

You have to respond to T queries of the following type

• Given L and R
• Find the product of all integers in the given list between L and R, inclusive
• Find the above product modulo some M, which is also given

# EXPLANATION

For each query, iterating through the list between L and R to maintain the modular products is too slow.

Of course, we use the fact that each value is between 1 and 100 to our advantage.

• There are 25 prime numbers between 1 and 100

Each number has a unique prime factorization. The product of a set of numbers can also be simulated by adding up the frequencies of each prime in all numbers in the set.

For example, suppose we have to multiply 36 and 45.

36 = 2232
45 = 325

36 * 45 = 22345


Thus, we can maintain a table of cumulative frequencies for each of the 25 primes between 1 to 100 for the given list of numbers.

When processing a query

• consider each of the 25 primes
• find the frquency of the prime between L and R. This can be done in O(1) using pre-calculation of cumulative frequencies
• calculate primefrquency for each prime and multiply these values
• maintain the result modulo M

These ideas are best presented in the pseudo code below.

# PSEUDO CODE

Given:
N, the number of numbers
L[N], the list of numbers
P[25], primes between [1, 100]
CF[N,25], cumulative frquency for each prime
for each query
Given Query: left, right, M
for i = 1 to 25
r = CF[right,i] - CF[left-1,i]
v = P[i]r % M, use repeated squaring


The complexity of answering each query would be O(25 log N).

Cumulative Frequencies can be calculated in O(25 * N).

# CODING COMMENTARY

You can either calculate the primes in thr porgram or hard code the array of primes by calculating it offline.

The repeated squaring should take care of the fact that the exponent can be 0. a0 should return 1 for any a.

Calculating the cumulative frequencies table should be done carefully. The frequencies of the primes for each number between 1 and 100 can be pre-calculated. Use these frequencies to build the cumulative frequencies table.

# SETTER'S SOLUTION

Can be found here.

# TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

2.4k128183169
accept rate: 14%

this is giving TLE anyone plz help http://ideone.com/pawz5b

(13 Aug '13, 23:26) 3★

I hate the question where key lies in exploiting the limits.

(18 Aug '13, 09:52)

 2 Is the setter's solution not up? I am getting a "Page Not Found". answered 12 Aug '13, 16:47 4.2k●5●23●64 accept rate: 15% "Page Not Found" when tying to access Setter' Solution (17 Aug '13, 11:59) @gamabunta Please look into this! (28 Aug '13, 16:19)
 1 D&C solution gives TLE. Complexity should be O(lg n) per query. Does anyone know why it is? Maybe in this approach MOD (%) operator is called fewer times... answered 12 Aug '13, 15:39 70●3●4●7 accept rate: 16% I don't see how D&C would apply to this problem, furthermore per query time complexity after all those pre-computation should be O(25) = O(1). (12 Aug '13, 15:41) tyrant2★ solution(l, r) = solution(l, m) * solution(m,r), where m is (l+r)/2 something like that should be D&C, the simplest one. and you are wrong about O(1), it is O(lg n), you have forgotten fast squaring .... (12 Aug '13, 15:49) @kingarthurie: My bad :( (12 Aug '13, 16:00) tyrant2★ If you're doing a naive D&C, query complexity would be Omega(n). Your recurrence would be T(n) = 2 * T(n / 2) + Omega(1). T(n) = Omega(n) (14 Aug '13, 06:21) michaelx4★
 0 The idea of my solution is to use segment tree to calculate sums in [L, R] interval for log10 values and later convert this to integer applying modulo operation. When I had such sum, first I found how many billions are in result (log10 / 9.0) exponentially multiplied this value and result multiplied with 10^dif where dif is something like real mod after dividing by 9.0 (hope it's clear). answered 12 Aug '13, 15:42 16.9k●49●115●225 accept rate: 11% I can up to that idea 2, but this idea give me WA if I'm correct, most probably duo floating point precision problems. (12 Aug '13, 15:51) 1 Truth is, that I didn't get accepted yet, but I don't think that there is precision problem. I'll let you know, when (if) I'll get AC ;-) (12 Aug '13, 16:10) Even I thought of segment trees. But then, for each modulo, creating a separate segment tree is wasteful. Having a segment tree with big integers is also wasteful (and probably give memory error). But I noticed that there are no changes in the array to be made. So, if i have an array prod of bigints, so that prod[i] = product of all number from a[1] to a[i], then product from a[l] to a[r] could be found in O(1) as prod[r]/prod[l - 1]. But the time required to do the math with bigints was too costly, and i got a lot of TLEs. I, however, managed to find the idea of primes and got an AC. (12 Aug '13, 16:29) Using Segment tree was my second approach as storing product of numbers in a product array table was my first approach but I was getting SIGFPE and then latter on WA on both the approaches. What I was thinking is the highest value of mod is 10^9 and during calculation of product or formation of segment tree, i was taking mod of 10^9 + 7 in order to maintain in the size in int. (13 Aug '13, 08:56) hrculiz1★ After then I thought perhaps this is not good idea so I implemented big integers in c++, this was my first handshake with big integers in c++, thanks to the problem I learned how to use big integers but still problems was not solved and got TLE's. (13 Aug '13, 08:56) hrculiz1★ there so much bilions in result such that owerflow in even long double : result = k * 1e9 + x ; result <= 1e200,000 so , k <= ~1e20000 (13 Aug '13, 20:37) But when using log10, the max sum is 10.000 * 2 ;-) (13 Aug '13, 20:40) showing 5 of 7 show all
 0 I use the same Algorithm as you do..but i got TLE ... answered 12 Aug '13, 15:59 6★jtjl 1 accept rate: 0% It's probably because your power function is recursive...make it iterative and submit it again....Happened same to me... Very tight Time Limit Constraint... (12 Aug '13, 16:19) i'd got AC and my power function was recursive too :D (12 Aug '13, 20:58) akrai482★ so was mine ! (13 Aug '13, 19:14)
 0 I used the same algo but instead of 25 i used all the 100 numbers that is O(100log(n)) solution , but it got TLE , can anyone explain why it is so , as time is independent of constants so O(100log(n)) should be same as O(25*log(n)) . answered 12 Aug '13, 16:04 3★m_garg 31●1●2●5 accept rate: 0% Time is not independent to constants. This was time tight solution, and you must use prime decomposition in order to get AC. You did 4 time more job, and your solution was 4 time slower. Usually it is not the problem, but in this problem, 4 times slower running time is a lot. (12 Aug '13, 16:09) 1 The O(25 log N) solution takes only 1/4-th of the time that O(100 log N) solution takes. So, that is obviously way faster. Many, I think, failed to figure that out! :( (12 Aug '13, 16:36)
 0 this is what i did...the similar approach..can be applid to solve.this problem wcount answered 12 Aug '13, 16:06 3★princerk 738●7●11●23 accept rate: 5%
 0 I dont think this question qualifies as an easy one. It requires segment trees and modular exponentiation. Please consider moving it to medium. It took me 3 hours to get to the Algorithm. And only around 900 could solve it over the ten days. answered 12 Aug '13, 20:09 1.3k●15●63●81 accept rate: 4% Segmented Trees ? Why do you need that in this ? (12 Aug '13, 21:55) @pushkarmishra : hi,could you please elaborate your idea of solving this problem using Segment Tree ? (14 Aug '13, 09:37)
 0 this is giving TLE anyone plz help http://ideone.com/pawz5b answered 13 Aug '13, 23:25 3★v2v4 1 accept rate: 0% @v2v4 I just changed the power function (exp in your code) and it is now AC. Here is the link http://www.codechef.com/viewplaintext/2582486 Your code is absolutely correct, it's just that your exponential function performs too much modulus operations. And modulus operations are computationally expensive. Happy Coding. (26 Aug '13, 21:26) viaan2★
 0 can sm1 plss check why m i getting WA for my code..... http://www.codechef.com/viewsolution/2529589 i m getting all right answers for as many test cases as i could think of....am i missing sm exceptional case?? answered 14 Aug '13, 02:22 62●1●1●4 accept rate: 20%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×14,487
×3,212
×279
×120
×27

question asked: 12 Aug '13, 15:19

question was seen: 11,252 times

last updated: 27 Jun, 17:31