NICARRAY - EDITORIAL

Not enough karma to reply to the comment in question so this is a separate comment.

@kal013 you’re missing a couple of edgecases - one with a continue and one where there are no -1’s but the sum of the array elements is less than s. Fix those 2 and it passes - see https://www.codechef.com/viewsolution/21278595.

Found the bug @kal013

you handled the corner cases incorrectly. I have corrected and submitted. Here you go. https://www.codechef.com/viewsolution/21279093

PS: Nice running time xD

OK so I too found out the bug in my solution. In my solution, if the initial sum was unequal to required sum and there were no -1 present, it would print sum of gcd instead of 0.

Basically you fixed the bug TC which I pointed out @taran_1407 ? :stuck_out_tongue:

I fixed two bugs. One which your solution pointed. Another one which I found.

I tried the same approach as the editorial. Here is my solution.
My solution is only getting partially accepted. Can someone help me?

Edit: I attached the wrong solution. This is the solution I’m talking about- https://www.codechef.com/viewsolution/21288324

But, the problem is, how do we know how many times a partition will appear. Fortunately, we can rush to Combinatorics :stuck_out_tongue: for answers. We can see, It is just the number of Distinct permutations of the given sequence. ??

You are only following that part of the editorial which will give 30pts. Follow editorial from above statement to fetch rest 70pts.

1 Like

I’m sorry I sent you the wrong solution. This is the latest solution I submitted with the idea you suggested- https://www.codechef.com/viewsolution/21288324

It is still giving 30 points. WHat do I do?

@aryanc403 can you help me?

Nevermind, I found my mistake. I was not using modular inverse while dividing by factorials.

2 Likes

I used dp,it got accepted
dp[N][S] denotes ans upto nth element such that its sum is S

dp3[N][S] denotes number of positive solution of x1+x2+…xn=S

dp2[N][element][Sum] denotes sum of gcd for all posible sequences: x1,x2…xn

Such that xi>=1,and sum(xi)=s

Like for 1 seq,it will be sum(gcd(xi,element))

Do this for all sequences and add…

complexity(50^4+T*(50^3))

https://www.codechef.com/viewsolution/21238180

2 Likes

At least explain your dp tables else no1s gonna understand it XD. Edit: Much better now :slight_smile:

1 Like

Thanks a lot @taran_1407 , @vijju123

3 Likes

looks like only I did it with same approach as editorial XD
everyone is coming up with DP

How to find the permutations of numbers of which sum is S ???
In the link mentioned in editorial it’s not defined precisely. Thanks in advance.

If we consider non-decreasing permutations, we can obtain them by a recursive function f(pos, sum_left, min) where pos is index of array, sum_left is the sum we want to achieve using remaining elements, and since we want our sequence to be increasing, our current element is never less than min, the element at position pos-1.

If we remove the min constraint, all we get will be any permutation having sum S, not only non-decreasing ones.

See count function in my solution. I first check if we have reach end of the array.

It not, I try to fill the remaining elements.

@taran_1407 Could you please explain how to solve this problem using Backtracking ?

Thanks

We assign a value to a position, say pos, try all possible values for all remaining positions (pos+1, n-1) and consider all cases.

Then, backtrack to pos-1 and again assign a different value at position pos and try all possible values for all remaining positions (pos+1, n-1) and consider all cases.

In case we know, from values assigned in range (0, pos) that no possible solution exist, we terminate immediately.

Read more about backtracking, especially the n-queen problem. https://en.wikipedia.org/wiki/Backtracking and https://www.geeksforgeeks.org/backtracking-introduction/