Hello Guys, I’m back with another set of editorials in hope you would like them.

I hadn’t attended December Cook Off due to certain unavoidable circumstances, that’s the reason of no editorials for Cook Off.

Without wasting much time, Here are the editorials.

### Days in a Month

##Difficulty: Cakewalk

### Problem:

Given number of days in month and Day as on 1st of given month, Find out frequency of each day.

### Solution:

Simplest solution use if else statements (Carefully though).

For W = 28, print 4 for each day, because a month with 28 days will always have 4 Sunday, 4 Monday ans so on.

For W = 29, print 4 for all days except the day of first of month. Example, for input 29 mon, ans is 5 4 4 4 4 4 4. (frequency of monday increased by 1).

For W = 30, print 4 for all days except the day of first of month and its next day. Example, for input 29 mon, ans is 5 5 4 4 4 4 4. (frequency of monday increased by 1).

For W = 31, print 4 for all days except the day of first of month and next two days. Example, for input 29 mon, ans is 5 5 5 4 4 4 4. (frequency of monday increased by 1).

For proof of above, You may consult Calendar.

Link to my

```
[1]
### Strange Function
##Difficulty:Easy
### Problem:
Given A and N, compute $f(A^N)$, where $f(X)$ is defined as:
- Compute the sum of digits of A; let's denote this sum by S.
- If S is a single-digit integer, then F(A) = S.
- Otherwise, F(A) = F(S).
### Solution:
FIrst thing, make a function $f(X)$ as described above. Now, Notice that we need to calculate $f(ans)$ where ans = $A^N$, we can directly compute $f( (f(A))^N)$. Doing this avoids the issue of overflow.
**Typical Modular exponentation Code** (Refer [geeksforgeeks][2])
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1){
res = (res*x);
if(res>=p)res = res%p;
}
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
Now, our solution is similar to modular exponentation, with one change. In Modular exponentation, we take res = (res*x); if res >= p, res = res%p;
But in our solution, we take res = res*x; $if(res >= 10) res = f(res)$. This way, we are calculating $f(A^N)$ in the same manner as Modular exponentation.
That's all.
Reason this works, is that suppose we have A = 15, N = 2.
$f((9+6)^2) = f(6^2 + 9*(9+12)) = f(36 + 9*21)$
Now, notice that the term 9*21 will never affect the value of our function.
For further proof, Calculate $f(9*X + Y)$ for some values of X and Y. The answer will always be Y.
Link to my
```

[3]

### Too Much Sweetness

##Difficulty: Easy-medium

##Prerequisites: DP

### Problem

Given two bags of sweets with p and q sweets respectively, Find out number of ways of eating **Exactly N** sweets, subject to conditions that

- ith bag cannot be opened more than Bi time.
- We cannot eat more than Si sweets in one step from ith bag.
- We need to choose bags alternatively.
- First bag can be either one.

### Solution;

Usually for counting problems where we need to make choices and count the number of ways of some sequence, we use DP, taking all the variables affecting answer in dp state.

DP state for this problem has been defined as

- N = Number of sweets to be eaten.
- p = Number of sweets in Bag 1
- q = Number of sweets in Bag 2
- B1 = Number of times we can open Bag 1
- B2 = Number of times we can open Bag 2

**Base Case** if(N == 0) return 1; if (N < 0, )return 0.

**DP recurrence**

if(last == 1)dp[N][p][q][B1][B2][last] = Summation from i = 1 to i = min(q,S2)solve(N-i, p,q-i,B1,B2-1, 2);

if(last == 2)dp[N][p][q][B1][B2][last] = Summation from i = 1 to i = min(p,S1)solve(N-i, p-i,q,B1-1,B2, 1);

Now, see what;s going on.

**In each recursive call, we eat i sweets from bag other than the last one, Reducing sweets to be eaten, also reducing Number of sweets in that bag, and reducing the max number of times of opening respective bag by one, Thus, meeting all conditions.**

Also, we run loop from 1 to min(p, S1) and 1 to min(q,S2), to comply with the condition of max sweets at a single step.

Also, use map in place of arrays which may give TLE. we do not visit all states.

Further We can also reduce state variables from 6 to 5 by noticing that N = P - p + Q - q where P and Q are input values while p and q is the current state values. But is didn’t use it ad still it works.

Here’s the link to my

```
[4].
Hope you all like my editorials.
On an important note, For further contests, One of my friend (@soham1234) has agreed to write unofficial editorials similar to mine. I hope you would like them too as much as you all do like mine.
[1]: https://www.codechef.com/viewsolution/16703802
[2]: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
[3]: https://www.codechef.com/viewsolution/16707912
[4]: http://codechef.com/viewsolution/16713650
```