# \[OFFICIAL\] Basic Math/Combinatorics Problems

**URL:** <https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236>\
**Category:** CodeChef-DSA-learners\
**Tags:** basic, combinatorics, math\
**Created:** [May 10, 2020, 4:06am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236 "2020-05-10T04:06:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidhant007](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sidhant007/32/24644_2.png) [@sidhant007](https://discuss.codechef.com/u/sidhant007)\
**Post date:** [May 10, 2020, 4:06am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/1 "2020-05-10T04:06:09Z")

</div>

Hi all,

Let’s have discussion about generic questions related to combinatorics, probability, expectation, etc. This can be run in parallel with the current weekly problem sets, since improving in Math is a long process. (Avoid using a computer to try to solve these questions, you should aim to make a closed form formula for these questions)

So I am uploading some questions here, if you have any good questions of similar taste in mind do share them here 🙂

We will first do some easier combinatorics questions, later on we can move to expectation, probability, recurrences, etc.

Incase you think you don’t have enough background in probability/combinatorics then refer to this place it is a good resource with good theory + good problems: [Brilliant.org discrete math section](https://brilliant.org/discrete-mathematics/)

1a. Given a 2d grid how many ways are there to go from (0,0) (bottom left corner) to (4,6) (top right corner) given you are allowed to move only right and up?  
1b. Count the number of ways to go from (0,0) to (5,5) when you allowed to move right and up. But you are also allowed to move one step left at most once ?

> **Solution**
>
> 1a. Think of going right as R and going up as U. Then your path is basically a 10 character string of 4Rs and 6Us. Say your string starts with “RUU”, then it means first step take a right then take two ups and so on. So how many ways are there to go from (0, 0) to (4, 6)? It is equal to number of ways to re-arrange the 4Rs and 6Us in 10 dashes, i.e C(10, 4).
> 
> 1b. TBA

2a. Number of palindromes using [a-z] of length exactly 7?  
2b. How about for general length = N?  
2c. How many recursively palindromic binary strings are there of length N? (A string “s” of length N is recursively palindromic is “s” is a palindrome and the first half of “s”, i.e from s[0] to s[N/2 - 1] is also recursively palindrome). For example for N = 5, there are four such strings {00000, 00100, 11011, 11111}, so answer is 4.

> **Solution**
>
> 2a. Length is 7 so treat it as 7 dashes. The first 3 dashes fix the characters for the last 3 dashes as well and the middle dash can be anything. So answer is 26^4.
> 
> 2b. For general N, extend this idea to 26^{\lceil \frac{N}{2} \rceil}. This is ceiling function, because for odd N, the middle dash provides extra 26 options, but for even there is no such middle dash.
> 
> 2c. TBA

3a. Number of factors of 840 (including 1 and 840 itself)?  
3b. Sum of all the factors of 840 (including 1 and 840 itself)?  
3c. Number of factors and sum of factors for a generic N?

> **Solution**
>
> 3c. Explaining the generalised version, can extend it for specific N = 840.
> 
> Let N = p\_1^{k\_1} \times p\_2^{k\_2} \times \dots = \prod\_{i = 1}^{m}p\_i^{k\_i}, where each p\_i is a unique prime number and k\_i \> 0 and m denotes the number of distinct prime factors of N, i.e this representation is the prime factorisation of N.
> 
> Now using this, you can see that the number of factors is (k\_1 + 1) \times (k\_2 + 1) \times \dots = \prod\_{i = 1}^{m}(k\_i + 1)  
> Proof: Your factor must be have its prime factorisation as a “subset” of the prime factorisation of N, i.e let K be a factor of N and its prime factorisation be p\_1^{q\_1}\times p\_2^{q\_2} \dots p\_m^{q\_m}, then q\_i \leq k\_i, \forall i \in [1, m].
> 
> Now each of the prime has to come lesser of equal number of times than it comes in N. So for p\_1 how many possibilities do we have? We have p\_1^0, p\_1^1, \dots, p\_1^{k\_1}, i.e (k\_1 + 1) possibilities. Similarly for a general p\_i we have (k\_i + 1) possibilities. Since each prime is independent for us, therefore answer is $\prod\_{i = 1}^{m}(k\_i + 1).
> 
> Sum of factors = (p\_1^0 + p\_1^1 + p\_1^2 + \dots + p\_1^{k\_1}) \times (p\_2^0 + p\_2^1 + \dots + p\_2^{k\_2}) \times \dots = \prod\_{i = 1}^{m}(\sum\_{j = 1}^{k\_i}(p\_i^j)) = \prod\_{i = 1}^{m}\frac{p\_i^{k\_i + 1} - 1}{p\_i - 1}
> 
> Proof: Think of the polynomial (p\_1^0 + p\_1^1 + p\_1^2 + \dots + p\_1^{k\_1}) \times (p\_2^0 + p\_2^1 + \dots + p\_2^{k\_2}) \times \dots = \prod\_{i = 1}^{m}(\sum\_{j = 1}^{k\_i}(p\_i^j)). Then try to actually manually work out each term of this expression. Notice that each term will be of the form p\_1^{a\_1} \times p\_2^{a\_2} \times \dots \times p\_m^{a\_m}, where this term is actually one of the factors of N. Also notice that each factor will come at least once and not be duplicated, i.e it will come exactly once. What this means is that that all the terms of this expression are all the factors. And all the terms are under addition, so they are giving us the same only. That is why the expression is equal to the sum of all the factors (including 1 and N)

4a. Given x + y + z = 10, x \>= 0, y \>= 0 and z \>= 0, how many integer solutions, i.e triplets of integer (x, y, z)'s are there satisfying the above system of equations?  
4b. How about x + y + z = 10, x \>= 1, y \>= 2 and z \>= 3. Now how many ?

> **Solution**
>
> 4a. Think of forming 10 as 10 stones and x, y, z as 2 sticks. So you have 10 stones and 2 sticks and you want to put these sticks in between these 10 stones which are present in a straight line. x will be equal to the number of stones between the start and the first stick, y will be the number of stones between first and second stick and z will the be the number of stones between the second stick and the end. Notice that solving number of non-negative solutions to x + y + z = 10 is now equal to the number of ways we can put these 2 sticks. So we have 10 + 2 = 12 dashes and we want to put these 2 sticks in between, i.e C(12, 2).
> 
> 4b. TBA

I hope to add more questions as and when I feel we can move to harder questions.

Feel free to have an attempt at these questions and share you solutions/explanations in the comment section below 🙂

_Update 1_: I saw a good response and will be working on adding more problems soon. Meanwhile I have added solutions to some of the parts. Please, please dont look at those solutions immediately after reading the question. Also TBA = To Be Announced.

---

<div class="post-metadata">

**Author:** ![ak408176](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@ak408176](https://discuss.codechef.com/u/ak408176)\
**Post date:** [May 11, 2020, 6:54am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/2 "2020-05-11T06:54:30Z")

</div>

i have formed the formula for 1 a …that is (m+n) C n . where m = row no. n = column no.  
i got this formula by doing these steps  
step 1 : we can calculate no.of ways to [i, j] just by summing no. of ways to [i-1, j] and no.of ways to [i, j-1] (becoz to go to [i, j] from these two points we have only one way which is right and up move)  
step 2: the first column and first raw will be filled by 1 only  
when i filled the grid i found out that the diagonal no. are binomial coefficient (diagonal with 1 as end points)(no in pascal triangle)  
and found the above formula.

---

<div class="post-metadata">

**Author:** ![bansal1999718](https://discuss.codechef.com/user_avatar/discuss.codechef.com/bansal1999718/32/3653_2.png) [@bansal1999718](https://discuss.codechef.com/u/bansal1999718)\
**Post date:** [May 13, 2020, 6:21pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/3 "2020-05-13T18:21:07Z")

</div>

@sidhant007 in 1b:  
in a way to reach from (0,0) to (5,5) can we visit a coordinate more than once?

and can u provide the link to the this question so that i can test my solution?

---

<div class="post-metadata">

**Author:** ![namancoder](https://discuss.codechef.com/user_avatar/discuss.codechef.com/namancoder/32/18957_2.png) [@namancoder](https://discuss.codechef.com/u/namancoder)\
**Post date:** [May 14, 2020, 10:34am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/4 "2020-05-14T10:34:25Z")

</div>

4a and 4b can be done by using formula n+r-1©r-1 formula

where n = value in RHS.  
and r = number of variables

for 4a-\>

10+3-1©3-1 = 12©2 = 66

for 4b-\>

we just make RHS value of inequation to 0;  
x-1\>=0 , y-2\>=0 , z-3\>=0;

NOW

x-1+y-2+z-3=10  
x+y+z = 4;

now the same formula as in (4a)

4+3-1©3-1 = 15

Used the simple terms to explain the problem,

popular formula

_C(n+r-1,r-1)_

---

<div class="post-metadata">

**Author:** ![master19](https://discuss.codechef.com/user_avatar/discuss.codechef.com/master19/32/81156_2.png) [@master19](https://discuss.codechef.com/u/master19)\
**Post date:** [May 17, 2020, 8:57pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/5 "2020-05-17T20:57:10Z")

</div>

For the :  
1a and 1b approach is the same !!  
We have to go from (x, y) to (a, b) then number of moves will be:  
(n+m)Cn where n = abs(a -x) and m = abs(b - y)  
2a: 26 _26_26\*26  
2b: power(26, ceil(num/2))

---

<div class="post-metadata">

**Author:** ![harshgarg\_32](https://discuss.codechef.com/user_avatar/discuss.codechef.com/harshgarg_32/32/24644_2.png) [@harshgarg\_32](https://discuss.codechef.com/u/harshgarg_32)\
**Post date:** [May 18, 2020, 10:35am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/6 "2020-05-18T10:35:20Z")

</div>

Ofcourse!

---

<div class="post-metadata">

**Author:** ![jalotra](https://discuss.codechef.com/user_avatar/discuss.codechef.com/jalotra/32/24644_2.png) [@jalotra](https://discuss.codechef.com/u/jalotra)\
**Post date:** [May 18, 2020, 1:32pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/7 "2020-05-18T13:32:05Z")

</div>

Intiution behind problem 4.  
4.a) Think that we have laid down 10 balls in a row and we want to partition these balls in 3 distinct bins. How to do that ? Not so intuitive use 2 sticks and place them somewhere in the row of balls. Profit : Now you can easily change the position of those sticks across the balls’ row and count different combinations that can occur or in problems’ terms integer solutions for x, y and z. How to find the total ways to partition these balls into 3 bins ? Easy just choose the positions of the two sticks and the task is done. Solution: (10 + no\_sticks\_required)choose(no\_sticks\_required)

---

<div class="post-metadata">

**Author:** ![jalotra](https://discuss.codechef.com/user_avatar/discuss.codechef.com/jalotra/32/24644_2.png) [@jalotra](https://discuss.codechef.com/u/jalotra)\
**Post date:** [May 18, 2020, 1:39pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/8 "2020-05-18T13:39:51Z")

</div>

Intuition behind Problem 1a:  
Notation : # --\> Number of  
Want to go from (0, 0) to (4, 6). Let’s say that each right move is defined as “R” and each up move is defined as “U”. A possible solution is UUUUUURRRR or RURUUURRUU. So see the pattern that #U’s + #R’s == 10. Now the problem can be formulated as choosing 6U’s or 4R’s out of string of len 10. Formula (#U’s needed + #R’s needed)choose(#U’s needed).

---

<div class="post-metadata">

**Author:** ![lightstar7910](https://discuss.codechef.com/user_avatar/discuss.codechef.com/lightstar7910/32/3653_2.png) [@lightstar7910](https://discuss.codechef.com/u/lightstar7910)\
**Post date:** [May 18, 2020, 1:59pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/9 "2020-05-18T13:59:04Z")

</div>

For 2a. we can go about the question like this.  
visualize the question as blank spaces for each characters like if we take the N in the question is 7. So the blank spaces would be like :-\> \_ \_ \_ \_ \_ \_ \_ so for the 1 st space it will have 26 choices for us to choose from and its corresponding character (the mirror image) will have to be the same character to make the word palindrome. And the question doesn’t say anything about using a character only once so we will have the answer as 26 \* 26 \* 26 \* 26 \* 1 \* 1 \* 1 (the 4 th ‘26’ is there cause it is at the middle and it wouldn’t affect the word being palindrome.

As for generalizing the solution 2.b we can write it as 26 \* (ceil(N/2) ). Cause the for any odd number it will be the same as i have explained above and for even numbers the word will always be divided into two equal parts so the ceil() function wouldn’t matter anyway. Its basically there to handle the odd cases.

Thank You, for reading hope it helped someone.

---

<div class="post-metadata">

**Author:** ![jalotra](https://discuss.codechef.com/user_avatar/discuss.codechef.com/jalotra/32/24644_2.png) [@jalotra](https://discuss.codechef.com/u/jalotra)\
**Post date:** [May 18, 2020, 2:06pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/10 "2020-05-18T14:06:59Z")

</div>

Problem 3:  
Factorise N into prime factors and then number of factors is the product of all nums that sits in the powers. The sum is the product of Gp of each prime pi from 0-to-Ki.

---

<div class="post-metadata">

**Author:** ![vivek\_shah98](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@vivek\_shah98](https://discuss.codechef.com/u/vivek_shah98)\
**Post date:** [May 19, 2020, 8:03am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/11 "2020-05-19T08:03:39Z")

</div>

1  
a) nCr(10, 4) = nCr(10, 6)  
b) For (5,5) ans is 5 \* nCr(12, 7) For general (n, m), ans is n \* nCr(n+m+2, n+2).

2  
a) 26^(3)  
b) 26^((n+1)/2)  
c) 2^(\_\_builtin\_popcountll(n))

3 840 = 2^3 \* 3 \* 5 \* 7  
a) No. of factors = (3 + 1) \* (1 + 1) \* (1 + 1) \* (1 + 1) =\> 4 \* 2 \* 2 \* 2 =\> 32  
b) Sum of factors = (1 + 2 + 2^2 + 2^3) \* (1 + 3) \* (1 + 5) \* (1 + 7) =\> 15 \* 4 \* 6 \* 8 =\> 2880  
c) No. of factors of N = p1^x1 \* p2^x2 \* … \* pn^xn =\> (x1+1)_(x2+1)_…\*(xn+1)  
Sum of factors of N = (1 + p1^1 + p1^2 + p1^x1) \* … \* (1 + pn^1 + pn^2 + pn^xn) =\> Each term can be computed using G.P  
4)  
a) nCr(12, 2) = nCr(12, 10)  
b) nCr(6, 2) = nCr(6, 4)

---

<div class="post-metadata">

**Author:** ![lightstar7910](https://discuss.codechef.com/user_avatar/discuss.codechef.com/lightstar7910/32/3653_2.png) [@lightstar7910](https://discuss.codechef.com/u/lightstar7910)\
**Post date:** [May 19, 2020, 12:41pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/12 "2020-05-19T12:41:55Z")

</div>

[DISCRETE MATHS](https://youtu.be/DBugSTeX1zw) Guys you can check this channel out. Its free and very well explained. Many topics of discrete maths which are required in CP sometimes are covered there.  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![sajal0709](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sajal0709/32/13713_2.png) [@sajal0709](https://discuss.codechef.com/u/sajal0709)\
**Post date:** [May 20, 2020, 7:39pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/14 "2020-05-20T19:39:01Z")

</div>

For 2 a) I think it should be 26^(4).

---

<div class="post-metadata">

**Author:** ![girishgarg](https://discuss.codechef.com/user_avatar/discuss.codechef.com/girishgarg/32/28183_2.png) [@girishgarg](https://discuss.codechef.com/u/girishgarg)\
**Post date:** [May 21, 2020, 11:30am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/15 "2020-05-21T11:30:44Z")

</div>

Can you add this grid problem?  
[https://hack.codingblocks.com/app/dcb/1271](https://hack.codingblocks.com/app/dcb/1271)

---

<div class="post-metadata">

**Author:** ![shameekagarwal](https://discuss.codechef.com/user_avatar/discuss.codechef.com/shameekagarwal/32/3653_2.png) [@shameekagarwal](https://discuss.codechef.com/u/shameekagarwal)\
**Post date:** [August 29, 2020, 12:24pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/16 "2020-08-29T12:24:30Z")

</div>

**can u please explain 2 c)**

**also 1 b) (i got the same answer just want to know approach)**

my 1b) -

(n+1 ‘R’) (1 ‘L’) (m ‘U’) but ‘L’ cant come first or last - so only n/(n+2) cases valid

```auto
    (n + m + 2)! n
    ------------ * -----
    (n + 1)! m! (n + 2)

```

---

<div class="post-metadata">

**Author:** ![sam000013](https://discuss.codechef.com/user_avatar/discuss.codechef.com/sam000013/32/29105_2.png) [@sam000013](https://discuss.codechef.com/u/sam000013)\
**Post date:** [October 7, 2020, 11:36am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/19 "2020-10-07T11:36:55Z")

</div>

My solution for 1b goes as follow:  
Since in the question it is given we can take atmost 1 left turn, this leads to two cases:

1. 0 left turns - 10C5 since the length of the solution as going by 1a’s technique by author would be 10 and we can choose 5R’s or 5U’s from 10 places.

1. 1 left turn - 12C6\*6 since the length of the solution would be 12, 1extra R will be needed to compensate for 1 L, so we can choose from 12 places 6R’s which is 12C6 multiplied by 6(for choosing 1 L from the remaining 6 places and U will there at the remaining places by default.)  
So the final answer will be the addition of both the cases.

10C5 + 12C6\*6

Please review my answer and rectify if needed.

---

<div class="post-metadata">

**Author:** ![devudon](https://discuss.codechef.com/user_avatar/discuss.codechef.com/devudon/32/24644_2.png) [@devudon](https://discuss.codechef.com/u/devudon)\
**Post date:** [November 30, 2020, 5:05am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/20 "2020-11-30T05:05:55Z")

</div>

but we cannot have L at first .

LRRRRRRUUUUU  
where L come first.  
i think this is also not possible  
RRRRRUUUUUL  
like moving all right and then all up and one left (so you can never reach your desired location )…  
rest we can do i guess . please someone now expain me derivation of what i wrote . i cant do it by myself.

---

<div class="post-metadata">

**Author:** ![rmsb3992](https://discuss.codechef.com/user_avatar/discuss.codechef.com/rmsb3992/32/25190_2.png) [@rmsb3992](https://discuss.codechef.com/u/rmsb3992)\
**Post date:** [January 21, 2021, 7:49pm UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/22 "2021-01-21T19:49:30Z")

</div>

@shameekagarwal 2 c. It can be done using dp.  
dp[1]=2// dp[i] denotes answer for i length string.  
dp[2]=2// u can calculate it.  
dp[3]:  
dp[1]\*(no of ways to add 0 or 1) \* 1(it will be same as first part)(( Treat the string in three parts i.e first part , middle part(length is 1) ,last part.For even length strings middle part will be 0)).  
2 \* 2=4  
dp[4]=dp[2] // since no way to add 0 or 1. i.e dp[2] \* 1 \* 1.  
dp[5] =dp[5/2] \* 2 \* 1(last part is fixed).=dp[2]\*2=4.  
You can move like it.You will see that there is a pattern i.e 2^(no of 1s)(The author to whom i am replying to, said that.I haven’t proved it.)  
Or, You can use dp to calculate it.  
Also for 1B,  
Let us say u have configuration RRRRUUUUL.It does not matter the order of occurrence of them. Say u encounter an “L”.What it does is that, it necessarily puts u one step back.  
suppose u are at 1( taking in account (1,0) right from (0,0).Now you move left ,coordinate now is (0,0) . It cancels 1 right .Thereby occuring two losses(if it would have been right i would be at (2,0) instead of (0,0)).Not sure about whether I am right, but who knows atleast this might help you.

---

<div class="post-metadata">

**Author:** ![wajiha\_20](https://discuss.codechef.com/user_avatar/discuss.codechef.com/wajiha_20/32/77502_2.png) [@wajiha\_20](https://discuss.codechef.com/u/wajiha_20)\
**Post date:** [April 5, 2021, 8:51am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/23 "2021-04-05T08:51:37Z")

</div>

by using recursion it is giving different answer…

---

<div class="post-metadata">

**Author:** ![nitinkumar1238](https://discuss.codechef.com/user_avatar/discuss.codechef.com/nitinkumar1238/32/335037_2.png) [@nitinkumar1238](https://discuss.codechef.com/u/nitinkumar1238)\
**Post date:** [August 26, 2021, 5:32am UTC](https://discuss.codechef.com/t/official-basic-math-combinatorics-problems/65236/24 "2021-08-26T05:32:08Z")

</div>

For 2c)  
T1 = 2, T2 = 2  
(n is odd) Tn = 2\*T((n-1)/2)  
(n is even) Tn = T(n/2)

Interstingly Tn = 2^(no. of set bits in binary represention of n) .  
Can someone explain the intution. ?
