# FILLBOX - Editorial

Problem Link : CodeChef: Practical coding for everyone

Let A be the array that consist of number of nodes ending at Q at a particular level
A = { 0, 1 , 1, 3 }

Let B be the array that consist of number of nodes NOT ending at Q at a particular level
B = {1, 1 , 3, 5 }

Convention : Favourable nodes ( ending at Q )
Non Favourable nodes (NOT ending at Q)

On carefull observation it may be noted that:

1. A[i] = B[i-1]
Reason :
All the favourable nodes ( ending at Q ) will only be produced by non favourable nodes of the previous level.

2. B[i] = A[i-1] X (K – 1) + B[i-1] X (K – 2)
Reason :
a) For A[i-1]*(K – 1) , some of the non favourable nodes are produced by favourable nodes of the previous level , multiply by (K – 1 ) as each favourable node will produce K-1 non favourable nodes

b) For B[i-1]*(K – 2) , rest of the non favourable nodes are produced by non favourable nodes of previous level, multiply by (K-2) , as one produced node is favourable, so we subtract 2 from this.

``````#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

class element
{
public:
long long int a;
long long int b;
};
int main()
{   long long int test;
cin >> test;
while(test--){
long long int n, k, p, q;
cin >> n >> k >> p >> q;
element *dp = new element[n];
// if the number of balls in the first box is same as in the last box
// then
if (p == q)
{
dp.a = 1;
dp.b = 0;
}
else
{
dp.a = 0;
dp.b = 1;
}

// dp approach as mentioned in the text
for (int i = 1; i < n; i++)
{
dp[i].a = dp[i - 1].b;
dp[i].b = (dp[i - 1].a * (k - 1)) % MOD + (dp[i - 1].b * (k - 2)) % MOD;
dp[i].b %= MOD;
}
cout << dp[n - 1].a << endl;
}
}
``````
3 Likes

Isn’t this problem almost same as this problem?

1 Like

Thanks, awesome editorial. but there is a typo in if(p==q) it should be dp.a=1 and dp.b=0;

2 Likes

There are millions of cp questions and many of them revolve around the same philosophy.
CP is practice. So the moto is ,you were not able to google questions and get the solution.

It is only the practice and presence of mind that will gear you up. 1 Like

@piyushbhutani But you should not give a copied problem in a public contest(with prizes/laddus).
because many person could have copied the solution from hackerrank.

1 Like

Yes, it is about practice, and I already had encountered this problem in hackerrank (and couldn.'t solve) while practicing some days ago, so seeing the problem it reminded me of the same and got the solution by googling this, but I didn’t submit the solution.

1 Like

I will pay heed to your words. This is our first contest. But this is not a copied problem.
Thanks buddy. I will learn from my this mistake. 2 Likes

If the above explanation is still unclear to anyone, follow this- Solution Video

Your diagram is a bit wrong, which can bring others into confusion, just update the node numbers

the node numbers shouldn’t be 2->3->3->(2 or 1)
but must be 2->3->2->(3 or 1)

1 Like

This is very much similar to Tetrahedron problem on codeforces.
{"ft_lang_label":"__label__en","ft_lang_prob":0.779974,"math_prob":0.94764984,"size":1625,"snap":"2023-14-2023-23","text_gpt3_token_len":531,"char_repetition_ratio":0.15237507,"word_repetition_ratio":0.09714286,"special_character_ratio":0.38215384,"punctuation_ratio":0.14550264,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9916856,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-04T16:56:41Z\",\"WARC-Record-ID\":\"<urn:uuid:729898f0-6d47-455d-bf0a-b3e5ef880b48>\",\"Content-Length\":\"36096\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:334b5f85-0a15-4a81-bba5-87473f21229b>\",\"WARC-Concurrent-To\":\"<urn:uuid:c3322d55-b981-41fc-8409-9600f5f8becc>\",\"WARC-IP-Address\":\"34.198.237.79\",\"WARC-Target-URI\":\"https://discuss.codechef.com/t/fillbox-editorial/62268\",\"WARC-Payload-Digest\":\"sha1:4GHPBXIEF22UFXGW5DTE6O4OQJNF2J6W\",\"WARC-Block-Digest\":\"sha1:FDTKWHITQQWYUTK46NFXY4RIU54HFU4Q\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224650201.19_warc_CC-MAIN-20230604161111-20230604191111-00133.warc.gz\"}"}