CHMOD - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

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
	answer = 1
	for i = 1 to 25
		r = CF[right,i] - CF[left-1,i]
		v = P[i]r % M, use repeated squaring
		answer = (answer * v) % M

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.

23 Likes

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…

1 Like

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).

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).

1 Like

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 …

I can up to that idea 2, but this idea give me WA if I’m correct, most probably duo floating point precision problems.

I use the same Algorithm as you do…but i got TLE …

@kingarthurie: My bad :frowning:

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)) .

this is what i did…the similar approach…can be applid to solve.this problem wcount

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.

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 :wink:

1 Like

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…

Took me ages to figure the idea! My solution: http://www.codechef.com/viewplaintext/2511516

First, I had a go at it using Python and Java, thanks to the support for big integers. But they gave me TLE.

Thinking “out of the box”, I tried the naive method. Actually find all products (keeping the modulo) in the given ranges. That too, timed out obviously.

Then I figured out that N is only as large as 100. Instead of storing the primes’ frequencies. I stored the numbers frequencies itself. A complexity of O(100 log N) per query and O(100 N) for pre-calculations. But this welcomed me with a shower of TLES.

Common, how else can I optimize? Multiplications… modulos… I thought of how to stop myself from looping, even the 100 times, just to avoid those computations. Couldn’t find any better solution. I thought, I am having modular exponentiation. If somehow I could increase the exponents, by combining some numbers, then I could further optimize. But couldn’t think of how to?

One day, thanks to the fundamental theorem of arithmetic, I remembered the prime numbers. Actually, I thought of storing the counts of powers of 2, as counts of powers of 2 itself. And just like that, the thought extended. I was not much thrilled, as the complexity now will be O(25 log N), but notation-wise, it is the same as O(100 log N), as both are O(log N). Still, I thought of 75% reduction in the running time.

Coded it up, but tests in my local machine could only bring down the running time (on average) from ~8.5 seconds to ~2 seconds per file. And we needed 1 second. But there is nothing bad as not trying. So, I tried, and voila! It was accepted.

That is my story of solving CHMOD.

What I wonder?

There are at least a few, who solved it at one go. I mean, how did they even think directly about the ideas of cumulative frequencies of prime numbers. Didn’t the idea of storing cumulative frequencies of the given numbers cross their mind? Or is there another idea?

Would love to hear from people who cracked this at their first attempt itself.

1 Like

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.

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! :frowning:

1 Like

Is the setter’s solution not up? I am getting a “Page Not Found”.

1 Like

I crack this problem in my first attempt :slight_smile:

2 Likes

@sandeepandey What was your approach? Is it the same as the one discussed in the editorial?

Yeah same approach. A(i) < =100 was big hint for me :slight_smile:

1 Like