Please share how you approached solving SANDWICH, even if you didn't get AC. It will help us to understand how to tackle more mathematically oriented problems. asked 18 May, 15:47

First thing which you have to see that Answer is $^{n+n*KN1}C_{n1}$ where $n=\lceil \frac{N}{K} \rceil$ Now for N as large as $10^{18}$ and $M$ nonprime, you will have to use the generalization of Lucas theorem for prime powers. And after calculating answer modulo all the prime powers present in $M$, you will have to use Chinese remainder theorem. Lucas theorem for prime powers is Theorem 1(page 2) in this research paper. answered 18 May, 16:14
1
how do u calculate ans modulo all prime powers in M .?? Since prime power can be non prime ( i am assuming prime power means p^q) So denominator and prime power can be non co prime too right ?? So how do you calculate modular inverse !!
(18 May, 16:56)
@sanket407 nCr % p^q has to be calculated using Lucas theorem for prime powers
(18 May, 17:01)
how ?? p^q is non prime right ?? lucas theorem works for only prime modulus right ?? Correct me if wrong !!
(18 May, 17:05)
it is, but in a paper of Andrew Granville, it has been extended to prime powers, and after that u need to use chinese remainder theorem
(18 May, 18:06)

I didn't use lucas theorem. I just solved it using CRT and by finding Modular multiplicative inverses. Note that if x! and (n1)! don't have any factors of p_{i} then they would have unique answered 18 May, 17:38
""Notice that we take out precisely (⌊x⁄pi⌋!)*pi out of the factorial"" Say x = 6. and p = 2. (⌊x⁄pi⌋!)*pi = 12 now. But 6! has 16 as power of 2 . Am i right or wrong ??
(19 May, 16:54)
1
@sanket407 The thing is we are taking out all the terms divisible by 2 out of 6!, not just the powers of 2.
(19 May, 18:14)
1
That's actually the generalized Lucas theorem (aka Granville theorem), seems like you were able to derive it during the contest, and that's brilliant!
(19 May, 21:50)
1
@hikarico I just read it. It really is the same. I actually didn't derive it completely on my own. Just didn't realize that this link that I referred to described the basic insight behind the generalized lucas's theorem. https://goo.gl/JLyjAa
(19 May, 23:35)
@equlnox Learnt , Coded And Passed !! Thanks !! btw your explanation was very good and code was well written too !! Easy to understand !!
(20 May, 12:24)

${\frac{n}{k}  {n \% k} + k} \choose {\frac{n}{k}}$ is the solution for the sandwich problem. Honestly this came as an observation rather than some Mathematics. Once this is identified, the problem becomes more of the optimization problem. Things you need for solving this.
Refer https://arxiv.org/pdf/1301.0252.pdf for General Lucas Theorem. Here is a implementation of CRT > http://www.geeksforgeeks.org/chineseremaindertheoremset2implementation Vote this answer, if you feel like. Karma needed for asking questions answered 18 May, 16:19
Can you please provide some source to read about Lucas Theorem and CRT?
(18 May, 17:14)
1
How did you identify that (nk−n%k+knk)(nk−n%k+knk) is the solution? I would appreciate if you can explain that as well :)
(18 May, 17:30)
1
@c0d3h4ck3d Updated the answer with resources for both. @vijju123 It was more of an observation, after writing a brute force algorithm.
(18 May, 17:48)
My brute force was too brute :/
(18 May, 18:15)
2
Here is how I derived the formula: https://discuss.codechef.com/questions/98129/yourapproachtosolvesandwich/98220
(18 May, 18:49)

Nobody seems to have shed some light on how to calculate $\binom{a}{b}\ mod\ (p^k)$. We don't need any research paper to calculate that just some group theory. First let us get rid of all the prime powers of p in $\binom{n}{r}$. Now let us consider a finite group where $X = \{ x : x < p^k, gcd(x, p^k) = 1 \}$. Now we can see that this is abelian group [commutative group] under multiplication. So, we can see that if we multiply all the elements of this group and take $mod\ (p^k)\ i.e\ x_1.x_2.x_3...x_i.. = 1\ if\ p^k = 4\ or\ p^k\ is\ power\ of\ odd\ prime\ and\ +1\ otherwise$. Now to calculate $n!\ mod\ p^k$[remember we've got rid of all the prime powers earlier], we see $n!\ \equiv (\pm 1)^{\dfrac{n}{p^k}}.(1.2.3..(n\ mod\ p^k)). (n / p)!\ (mod\ p^k)$. This is a simple recursive equation which we can calculate in $O(p^k\ log\ n)$ time. Similarly calculate $(n  r)!\ \&\ (r!)$. Take their inverses using extended euclidean and apply chinese remainder theorem. answered 18 May, 17:46
1
Can you shed some light on why that identity (the product of all integers from 1 to p that are coprime to p is either 1 or 1)is true? I don't understand how being an abelian group is the reason for it. Is this a known identity?
(29 May, 22:49)
1
Yes. Being an abelian group helps because it tells that the group operations are commutative in nature. So, we can take all the $x_i's$ with their inverses together and get the identity element from it. However, there will two elements in our group whose inverse element will be itself the element itself, we have to take care[which gives rise to +1], it'll be $1$ and $p^k  1$. Actually, this theorem is popularly known as Gauss's theorem who generalized Wilson's theorem on the finite groups. Here's a link to that. https://integermiscellany.wordpress.com/2015/07/23/generalizing_wilson/
(31 May, 08:33)

@shashank96,(https://discuss.codechef.com/questions/98129/yourapproachtosolvesandwich?page=1#98152) answered 18 May, 18:49

Sandwich is pure math. You can easily determine number of pieces you must cut your sandwich, let's call it T. Now imagine that you cut a sandwich of length l=k*T. That sandwich can be cut only one way and l>=n. Now to produce all ways to cut our original sandwich we must distribute (ln) "shortenings by one" between T pieces. That is "Stars and bars" problem https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics). Now you only need to know how to calculate C(n0, k0) modulo M for very large n0 and k0 (not n and k from a problem, but easily derived from them). For that we need to factorise M. Now suppose that M have some prime divisor p1 and M is divisible by pow(p1, k1), but not divisible by pow(p1, k1+1). Let n0!=pow(p1, t1)q1, where q1 and p1 are coprime. We need to calculate t1 and q1 modulo pow(p1, k1) for each prime divisor of M. (emaxx has similar algorithm, although not exactly one needed). After that we can use Chinese Remainder Theorem and reprsent n!=pow(p1, t1)pow(p2, t2)...pow(pi, ti)Qn, where Qn and M are coprime (we don't need Qn itself, only Qn modulo M). We must do previous part for all three factorials and then divide one by two others (subtract powers for prime divisors and divide in ring for Qn's) answered 18 May, 16:19

For 50 pts : Python users could use this inbuilt function scipy.misc.comb(N, R, exact=True) as mentioned in my solution : Python However for 100 pts the topic involved were 1. Lucas Theorem 2. Chinese Remainder Theorem 3. Wilson Theorem which gave an AC and can be referred by link mentioned in my Java code : here answered 18 May, 16:36

Two more links: Combinatorics  Mathematics Stack Exchange: nCr  HackerRank: http://m00nlight.github.io/algorithm/2015/04/11/hackerrankncr answered 18 May, 17:29
Thank you ymondelo20
(18 May, 17:37)

Hi guys! Here is a video editorial by @jtnydv25 on the problem. answered 29 May, 01:01

Basically you needed to use generalized lucas's theorem. First covert the solution to aCb form where Now, I could solve it for 50 pnts as I couldn't understand generalized lucas's theorem but here are some links to calculate nCr % m https://discuss.codechef.com/questions/3869/bestknownalgosforcalculatingncrm You can google for more. answered 18 May, 15:56
Thanks, mathecodecian :)
(18 May, 17:53)

After some observation, I found that the answer would be (N/K +R)cR where N is the length of the sandwhich and R=K(N%k). Wasn't able to solve it for when M was not prime tho as inverse could not be found. Here's my code (it's a bit messy tho): https://www.codechef.com/viewsolution/13530263 P.S: I just noticed we have the exact same rating(before the updation of this contests ratings.). What a coincidence xD. answered 18 May, 15:57
1
Ratings haven't updated.
(18 May, 16:02)
Ik, just in case he read that after the ratings were updated, it would've been weird.
(18 May, 16:09)
Thanks abdullah786 :D
(18 May, 17:44)

First I came up with a dynamic programming approach in O(N^3), then i test for pattern on small test. After that I realize f[i] = 1 with i<k or i%k==0, otherwise f[i] = f[ik] + f[ik+1] + ... + f[[i/k]*k1] which can be calculated in O(N). Still thinking for subtask 3 and 4... answered 18 May, 16:00

Answer was easily reduced to N C K mod m , where m is not prime . If m would have been prime , it would be easily solvable by lucas theorem . But here m is not necessarily prime. So, there were various algorithms to do that too. I am not sure since i got 50 only. One idea was to break m into it's prime factors. Let us say: m = p1^k1 * p2^k2 * p3^k3.... Now we can find answer to each factor by "generalized lucas theorem" and combine answers by CRT. source: https://discuss.codechef.com/questions/3869/bestknownalgosforcalculatingncrm Another approach is discussed here : http://m00nlight.github.io/algorithm/2015/04/11/hackerrankncr answered 18 May, 16:04
But CRT only works for square free numbers right?
(18 May, 16:07)
I am not quite sure , but i think CRT works fine if N and m are co prime. See the link in source. Here's a quote from that link: "You can find the result of nCr % m for each m = 27, 11, 13, 37. Once you have the answers, you can reconstruct the answer modulo 142857 using Chinese Remainder Theorem. These answers can be found by Naive Methods since, m is small."
(18 May, 16:18)

For
Subtask 1:
Brute force nCr calculations would work. No overflow worry. Easy 10 points. answered 18 May, 18:13

I did it using inclusion exclusion principle here ie we distribute balls( = nk) into boxes ( ceil(n/k) ) with maximum of k balls in each box and to calculate this I used 1. Lucas Theorem 2. Chinese Remainder Theorem. The approach for this is explained here (Q.2  Life is Strange 4th subtask). But sadly I could pass only 3 subtasks, I couldn't figure out the shorter version of formula. Link for my solution. answered 18 May, 23:41

@ista2000 WA was because of overflow only!! See this: https://www.codechef.com/viewsolution/13632506 There was overflow happening where you calculate the value of ret in f function. I just placed %x more cautiously. answered 19 May, 01:20

Here you can find a decent explanation and code in c++ and python for the nCr (large n and r) problem which includes handling the case of prime powers :). I also found the paper by Andrew Granville which seems to explain the same but it was too complex for me to understand. I used the code found in this link but gave the guy full credit in the code :). I learned something in this contest which is cool :). Hope it helps. http://m00nlight.github.io/algorithm/2015/04/11/hackerrankncr answered 19 May, 08:11

I have used Generalized Lucas theorem and CRT to solve the problem using the same paper mentioned by some users who have already answered in this thread. I don't know why I get WA on only 1 test case. Throughout the contest I have had 30 submissions for this problem with different changes in code to avoid overflows and all but I still could not get it. Please, can someone tell me what is wrong with my code? Thank you in advance to anyone who tries to look through my code. :) answered 19 May, 00:54
@prakhariitd Thank you. I lost 50 pts for a small bug :P
(19 May, 01:45)
sad for u @ista2000
(19 May, 01:50)

Given N,K Now, minimum number of partitions you can have is P = ceil(N/K), Now what is the maximum size of sandwich can be made with P partitions ? That is P*K, suppose initially all partitions are full, for example if N=10 and K=4, we have P=3 and partitions are like this : ............ now we have 3 groups each is fully filled. Now coming to the question, we need to delete some '.' from these groups in order to get a valid configuration in which total size of sandwich is N, suppose if we add a 'X' to any group we reduce its size by 1. Now the current size is answered 19 May, 14:38

https://www.hackerrank.com/contests/hourrank20/challenges/pstring/editorial There was similar problem on hackerank. And the link is editorial to that problem which gives detailed explanation about how to implement ncr % m, whether m is prime or composite; answered 01 Jun, 11:26
