You are not logged in. Please login at www.codechef.com to post your questions!

×

KSPHERES - Editorial

Problem Link

Practice
Contest

Difficulty

Easy

Pre-requisites

Dynamic programming

Problem

Count the number of ways to construct a sequence of nested spheres

How to get 40 points

First of all, let's note that the number of ways construct a sphere of the radii of R is equal to the product of the number of lower hemispheres of the radii of R multiplied by the number of upper hemispheres of the radii of R. Let's call this number S[R].

Now, let's denote the number of ways to construct a sequence of K nested spheres with the largest sphere having the radii of R by F[K][R]. Let's take F[0][0] equal to one.

The rules for the recalculation of f[K][R] are straightfowrward: we need to pick a sequence of (K-1) spheres, where the largest sphere has the radii less than R. The number of such sequences (let's denote it by W[K][R]) is equal to the sum of F[K-1][J] for all J < R. Now we need to add a sphere of the radii R to the end of each of these sequences. Considering that there are S[R] ways to create a sphere of the radii of R, we obtain that F[K][R] = W[K][R]*S[R].

The number of sequences of K nested spheres will clearly be equal to the sum of F[K][J] for all J between 1 and C.

In total, we need to calculate O(C2) states of F[][], and it takes O(C) to calculate each of them. So, the total complexity will be O(C3).

This solution is capable of solving the first two subtasks, but is too slow to solve the last one.

How to get 100 points

The solution's idea remains the same.

Let's find a way to calculate the values of F[][] in O(1) time. For achieving this, let's note that the value of W[K][R] equals to the W[K][R-1] + F[K-1][R-1]. So now, using the old values of W[][], we can calculate the new one in O(1).

Now, since the calculation of every state runs in O(1), we get the total complexity of O(C2), which fits for our problem.

Setter's Solution

Can be found here

Tester's Solution

Can be found here

This question is marked "community wiki".

asked 06 Oct '15, 07:07

xcwgf666's gravatar image

6★xcwgf666 ♦♦
719436377
accept rate: 0%

edited 15 Jan '16, 13:31

admin's gravatar image

0★admin ♦♦
19.8k350498541

Hi,

Can someone please tell me why my code is failing only in the second last test-case? https://www.codechef.com/viewsolution/8549224

Thanks :)

(15 Oct '15, 01:53) codedecode01115★

This one is my favorite question in the OCT15 challenge. By some examinations, I knew for a given c, we need to find the sum of product of elements in every subset of size c+1. By googling, I came across a generating function for the solution. I had never coded a polynomial multiplication before. Using a naive n^2 multiplication I was able to solve it. But, instead of finding the result modulo 10^9+7 I found modulo 10^9+9. After getting WA, I tried to come up with another logic and came up with the dynamic programming solution as given above (solving dp by myself gives a lot of pleasure :)). After solving it with dp approach, I tried debugging the first generating function approach and found my mistake. Then I got AC for that.

Thanks to the initial mistake, I solved a DP problem.

My dp approach : https://www.codechef.com/viewsolution/8464303 My other approach : https://www.codechef.com/viewsolution/8464356

link

answered 12 Oct '15, 15:21

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

hi dragonemperor, i have been following your posts on codechef long contest ques, it's been a great learning curve, i learned trie with the help of your amazing insights on REBXOR form sep15, i googled for polynomial function method but only managed to find FFT, can you please share the resource that led you to succesfully implement your code, thanks ..:)

(14 Oct '15, 09:32) rb082★
1

A little experiment using pen and paper showed that if first term is present x time, second y times and so on, (ignoring the terms present 0 times), the problem can be reduced to finding product of elements of of subset of side c+1 (and for all other values of c, same thing applies), in the array {x,y,...}

Following this link I got the approach

http://stackoverflow.com/questions/10106193/sum-of-product-of-subsets

I explained my approach in the code as a separate answer as this didn't fit comment. Check that out too.

(14 Oct '15, 13:36) dragonemperor3★

thankyou geeks:D

link

answered 26 Dec '15, 00:43

archit910's gravatar image

2★archit910
820212
accept rate: 3%

First find the frequency of upper hemisphere and lower hemisphere in two separate arrays.

Multiply each array index correspondingly and make copy of this array as in code.

Take cumulative sum from end of array and multiply this array by one shifting with copy array in the same mul array.

Sum of c-1 elements of mul array is D+1 answer where D=1. Then again take the cumulative of mul array and multiply this with copy array with 2 shifting.

Sum of c-2 elements of mul array is D+1 answer where D=2. Repeat this process by increasing shifts c-1 times

link

answered 26 Dec '15, 01:02

aparna0611's gravatar image

3★aparna0611
412
accept rate: 0%

I solved it without using DP.Only by Simple logic.
My code is here

Solved like ad-hoc problem.

link

answered 12 Oct '15, 15:55

abhishek20113's gravatar image

5★abhishek20113
393
accept rate: 0%

explain plz

(12 Oct '15, 23:55) nil962★
1

First find the frequency of upper hemisphere and lower hemisphere in two separate arrays.

Multiply each array index correspondingly and make copy of this array as in code.

Take cumulative sum from end of array and multiply this array by one shifting with copy array in the same mul array.

  • Sum of c-1 elements of mul array is D+1 answer where D=1.

Then again take the cumulative of mul array and multiply this with copy array with 2 shifting.

  • Sum of c-2 elements of mul array is D+1 answer where D=2. Repeat this process by increasing shifts c-1 times.

-> now you can understand code.

(13 Oct '15, 01:02) abhishek201135★
Answer is hidden as author is suspended. Click here to view.

answered 12 Oct '15, 19:16

sarvagya3943's gravatar image

4★sarvagya3943
(suspended)
accept rate: 36%

@rb08 Actually I also found only FFT and I didn't understand it. I am still trying to understand that.
For the above problem, I only needed to multiply n linear polynomials that made it a bit easy (I don't know if I could do for higher power).

Here's my approach.

Consider the polynomial (x-1)(x-2)
It is equal to x^2-3x+2
For any multiplication we only need the co-efficient of the polynomial (let's store only the co-efficient in array say coeff). Also, when we are multiplying the first polynomial with x, since it's co-efficient is 1, the coefficients of the result doesn't change. Only power of each term increases by 1.
Let's write x-1 as 0
x^2 + 1x - 1. When we are multiplying it with x, the result becomes 1x^2 - 1x +0. The power increases and co-efficient remains same.
When the constant factor is multiplied, the co-efficient change, not the power of each term. (x-1)
(-2) = -2*x + 2 (power of each term is constant). Now after multiplication of each part, we need to add the result.
The coeff array before multiplication is {0,1,-1} //0 for x^2
After multiplying x, since the power increases, we can simply shift them to the left as {1,-1,0} and then add the result of the second multiplication with the constant. This result has power less than the first multiplication result. So we start adding the terms starting from the second term (check below to understand).
Co-efficient after multiplication with x, {1,-1,0}

Co-efficient after multiplication with -2, {0,-2,+2}
Ignoring the first term which is always 0 for second result, and un-altered for first result, we add the remaining corresponding terms. So result is {+1,-3,+2}.
This is the basic approach. Hope you understood.

In my code, I didn't use multiple arrays for multiplication. I used a single long array initialized to 0. The first element of the array is the co-efficient for the highest power term. (whatever the highest term is after each multiplication). I used a pointer
idx to indicate the last position of the result (co-efficient of power 0, i.e. the constant term). For each multiplication, after multiplying with x, simply increased idx by 1. The new added term is always 0 as initialized. While multiplying the constant part, we need to add the result to corresponding terms. Remember, even though we shifted the co-efficient to the left, their value didn't change. We can re-use them for the multiplication again. See below

idx is pointing to last element. So the element before it is equal to the co-efficient of power 0 before multiplying the x. We need to multiply the constant term -2 to this co-efficient and add it to the term at idx (see it using pen and paper for better understanding). I did the following to accomplish this

coeff[idx]+=(coeff[idx-1]*constant_term)

Putting it in a loop, we get
for(int i=idx;i>0;i--) //i!=0 as 0th term is the highest power and it's co-efficient never changes
coeff[i]+=(coeff[i-1]*constant_term)

I used modular arithmetic on this.

link

answered 14 Oct '15, 13:32

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

thanks alot for a detailed explanation @dragonemperor, though i still need some time to grasp polynomial multiplication better. awsome work .. :)

(14 Oct '15, 19:45) rb082★

It can be solved in clogn using FFT(Polynomial multiplication)..I implemented it here https://www.codechef.com/viewsolution/8318645

But i messed up with the modulo thing..Can anyone please figure out why it is giving WA in few test cases in this code...

link

answered 12 Oct '15, 15:15

sandeep9's gravatar image

3★sandeep9
4782827
accept rate: 4%

Wherever you are doing subtraction add mod value to it i.e. 1000000007. Like in a[i+half] = even[i] - twiddle change it to a[i+half] = even[i] - twiddle + 1000000007. Sorry i am unable to confirm if it will give AC because i don't know vectors but i too was getting WA in nearly same test cases and got AC by adding mod value where i was doing subtraction.

My WA submission - https://www.codechef.com/viewsolution/8456126 My AC submission - https://www.codechef.com/viewsolution/8456131

(12 Oct '15, 16:11) dushsingh19954★

Can anybody take a look at my solution please https://www.codechef.com/viewsolution/8430787 I used the same dynamic approach as the OP but I get a wrong answer on one of the test cases.

I suspect it maybe a miscalculation with the modulus.

Any help will be much appreciated, thanks.

link

answered 12 Oct '15, 15:22

ricola86's gravatar image

1★ricola86
474125
accept rate: 0%

1

@ricola86 While adding currCount to prev(prev+=currCount) , prev can go out of range of int. So,just take it as unsigned long long and just apply mod operation on it. i.e

prev = ((prev%PRIME)+(currCount%PRIME))%PRIME;

Just this and you will get ac!!!

Link to your changed solution, https://www.codechef.com/submit/complete/8531991

(12 Oct '15, 17:54) go_on4★

Oh my god, I'm such an idiot. I always make stupid mistakes like that. Thanks very much for taking the time and having a look :-)

(12 Oct '15, 18:20) ricola861★

Happy to help.

(12 Oct '15, 18:42) go_on4★

i am getting wa in only one subtask out of 14 .i am not able to figure out the cause.please help. my code is here https://www.codechef.com/viewsolution/8456023

link

answered 12 Oct '15, 18:05

sid9406's gravatar image

2★sid9406
11
accept rate: 0%

1

@sid9406 Change "long int" to "long long int"
As a long int is a signed integral type that is at least 32 bits, while a long long or long long int is a signed integral type which is at least 64 bits

(12 Oct '15, 18:28) go_on4★

@sid9406 At various places your code might go out of the range of long int and hence cause overflow. You are required to use long long int instead. Here is the link to your updated solution. https://www.codechef.com/viewsolution/8532367

(12 Oct '15, 18:36) raunak_parijat4★

thank you very much ,it worked

(12 Oct '15, 22:36) sid94062★

Hello, I am a newbie here and never played with DP or modulus thing before. So i learned about modulus from somewhere and implemented this solution. Its getting WA on some cases. I think there is some problem with my 10^9+7 implementation. Can anyone please figure it out? I would be so thankful

link

answered 12 Oct '15, 18:59

torque's gravatar image

6★torque
4271111
accept rate: 17%

edited 12 Oct '15, 19:00

just try adding mod where you are using subtraction at each step :-). Mine was the same mistake.

(12 Oct '15, 21:57) ansh1star0333★

Can anyone tell what was the problem with my solution? Was able to pass few files of 2nd and 3rd subtask.Tried alot to get pass all the files,but i think some problem with modulo operation.Please tell me where was I missing something..here's my solution.Would be very thankful.

link

answered 12 Oct '15, 21:44

ansh1star033's gravatar image

3★ansh1star033
206129
accept rate: 9%

edited 12 Oct '15, 21:47

Well,i found my mistake by reading above comments.I'm a total fool,should have added mod while performing subtraction where ever required.

(12 Oct '15, 21:55) ansh1star0333★

I am getting WA in only one of the test cases. I am really stuck. I have tried almost everything. Somebody please help. Here's the link to my code.. https://www.codechef.com/viewsolution/8534532

link

answered 12 Oct '15, 21:59

sbansal3096's gravatar image

5★sbansal3096
11
accept rate: 0%

edited 12 Oct '15, 22:01

@torque (a-b)%m is NOT (a%m - b%m)%m instead it is (a%m - b%m + m)%m

link

answered 12 Oct '15, 22:03

atulshanbhag's gravatar image

4★atulshanbhag
22629
accept rate: 9%

Can anyone plz take a lokk at my solution, i am getting WA in just one testcase. https://www.codechef.com/viewsolution/8520392

link

answered 12 Oct '15, 22:42

fkhan_iitbhu's gravatar image

2★fkhan_iitbhu
1
accept rate: 0%

@fkhan_iitbhu As stated above please change int to long long int since large values of int cause overflow and then try again. :)

(12 Oct '15, 23:31) raunak_parijat4★

@fkhan_iitbhu use long long int everywhere

link

answered 12 Oct '15, 22:46

atulshanbhag's gravatar image

4★atulshanbhag
22629
accept rate: 9%

edited 12 Oct '15, 22:47

@atulshanbhag could you please tell me the reason for use long long everywhere in fkhan_iitbhu solution. Where his solution is getting an overflow.

link

answered 12 Oct '15, 23:25

runs's gravatar image

2★runs
11
accept rate: 0%

This was my First DP problem. Thanks to Problem Setter, this was really an amazing problem! :)

link

answered 13 Oct '15, 00:28

rishabhprsd7's gravatar image

2★rishabhprsd7
1.9k11243
accept rate: 14%

I tried quite a few different approaches for this problem but the first one I used is this. https://www.codechef.com/viewsolution/8373392 I basically used the previous values to ith sequence to get the values of the (i+1)th sequence but yet couldn't find why I was getting WA in particular cases. Could someone please help me out?

link

answered 13 Oct '15, 12:21

raunak_parijat's gravatar image

4★raunak_parijat
8726
accept rate: 0%

Just add modulo everywhere because there can be overflow. AC code - https://www.codechef.com/viewsolution/8537624

(13 Oct '15, 13:10) dushsingh19954★

Thank you so much. Now I finally understand why I was getting a WA.

(25 Oct '15, 19:27) raunak_parijat4★

Nice problem , it made me to think for a sol of polynomial multiplication ... which would not be possible , if i had studied DP earlier

This problem can be solved by multiplying two polynomial iteratively. by taking 1 polynomial as (1 + a[i]x ) and other as product obtained earlier ...

Here's link to my code... https://www.codechef.com/viewsolution/8434494

link

answered 13 Oct '15, 19:44

aditya9211's gravatar image

3★aditya9211
1
accept rate: 0%

I am getting 15 points and WA in some of the subtasks ,please can somebody take a look at my solution and tell me what is the problem here https://www.codechef.com/viewsolution/8370715 .It would be a great help,as this is the first time I got some points in a DP question :)

link

answered 14 Oct '15, 22:33

gauraaaaav65's gravatar image

2★gauraaaaav65
1
accept rate: 0%

I didn't understand why F[0][0] is taken '1' and not '0' in the editorial. Also what will be the base case for W[][]? Is F[0][0]=1(if it is correct) enough to solve the problem ?

link

answered 25 Oct '15, 19:11

akhileshydv20's gravatar image

2★akhileshydv20
173
accept rate: 0%

solved it with much simpler approach... https://www.codechef.com/viewsolution/8637581

(25 Oct '15, 20:15) akhileshydv202★

Getting wrong answer only at one of the parts of subtask 3 https://www.codechef.com/viewsolution/9015974 Any help would be appriciated

link

answered 25 Dec '15, 20:18

vaibhav1224's gravatar image

4★vaibhav1224
1
accept rate: 0%

Hey I tried to use long long int instead of long int in your code at some places and it gave 100pts. There was an overflow in your code. https://www.codechef.com/submit/complete/9019061

(26 Dec '15, 13:12) apptica5★

First of all, let's note that the number of ways construct a sphere of the radii of R is equal to the product of the number of lower hemispheres of the radii of R multiplied by the number of upper hemispheres of the radii of R. Let's call this number S[R].

Now, let's denote the number of ways to construct a sequence of K nested spheres with the largest sphere having the radii of R by F[K][R]. Let's take F[0][0] equal to one.

The rules for the recalculation of f[K][R] are straightfowrward: we need to pick a sequence of (K-1) spheres, where the largest sphere has the radii less than R. The number of such sequences (let's denote it by W[K][R]) is equal to the sum of F[K-1][J] for all J < R. Now we need to add a sphere of the radii R to the end of each of these sequences. Considering that there are S[R] ways to create a sphere of the radii of R, we obtain that F[K][R] = W[K][R]*S[R].

The number of sequences of K nested spheres will clearly be equal to the sum of F[K][J] for all J between 1 and C.

In total, we need to calculate O(C2) states of F[][], and it takes O(C) to calculate each of them. So, the total complexity will be O(C3).

This solution is capable of solving the first two subtasks, but is too slow to solve the last one.

link

answered 26 Dec '15, 00:52

wompowe's gravatar image

2★wompowe
573
accept rate: 0%

First of all, let's note that the number of ways construct a sphere of the radii of R is equal to the product of the number of lower hemispheres of the radii of R multiplied by the number of upper hemispheres of the radii of R. Let's call this number S[R].

Now, let's denote the number of ways to construct a sequence of K nested spheres with the largest sphere having the radii of R by F[K][R]. Let's take F[0][0] equal to one.

The rules for the recalculation of f[K][R] are straightfowrward: we need to pick a sequence of (K-1) spheres, where the largest sphere has the radii less than R. The number of such sequences (let's denote it by W[K][R]) is equal to the sum of F[K-1][J] for all J < R. Now we need to add a sphere of the radii R to the end of each of these sequences. Considering that there are S[R] ways to create a sphere of the radii of R, we obtain that F[K][R] = W[K][R]*S[R].

The number of sequences of K nested spheres will clearly be equal to the sum of F[K][J] for all J between 1 and C.

In total, we need to calculate O(C2) states of F[][], and it takes O(C) to calculate each of them. So, the total complexity will be O(C3).

This solution is capable of solving the first two subtasks, but is too slow to solve the last one.

link

answered 26 Dec '15, 00:53

wompowe's gravatar image

2★wompowe
573
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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:

×15,852
×3,820
×2,214
×103

question asked: 06 Oct '15, 07:07

question was seen: 7,153 times

last updated: 15 Jan '16, 13:31