## Editorial for TLE '17 Contest 4 P5 - Pascal's Tree

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: d

For the first subtask, it is enough to generate the row of Pascal's Triangle, then print out the elements of the row modulo . Alternatively, you can use the formula for the combination, and then print this number modulo . The formula for the combination is given below:

Time Complexity: Slower than

For the second subtask, it is enough to generate the row of Pascal's Triangle by storing the elements of each previous row modulo . Alternatively, you can use the formula for the combination in a data type that supports arbitrary-precision integers, such as Java's BigInteger or Python's normal integer, then print this number modulo .

Time Complexity:

For the third subtask, we first notice that is prime and . This means that no number in the row is divisible by . Since is prime, we can use Fermat's Little Theorem to get the multiplicative inverse. If is not divisible by , then , and we have that is the multiplicative inverse of .

Why do we need the multiplicative inverse? Suppose that you have , you know the value of , and you want to find . You would compute . To divide by , you instead multiply by .

Next, suppose that we have already computed modulo and we want to find . We can instead find:

The time complexity of this computation is , and you should repeat this computation times.

Make sure that 64-bit integers are used, and no overflows will occur.

Time Complexity:

The fourth subtask is a distraction. This time, we could have , and this would result in some elements being divisible by . I have found a few online articles that point to Lucas's Theorem, which could be helpful.

For the fifth subtask, we will first look at the relationship between two adjacent combinations.

The proof of this relationship is very boring. I recommend that you try to prove it! Hint: expand everything.

A combination can always be factored as a product of prime numbers. Also, two adjacent combinations share many of the same prime factors. We want to create a multiset that supports these 3 operations:

• Insert a prime number
• Erase an existing number in the multiset
• Calculate the product of the numbers in the multiset modulo

This data structure is easiest to implement as a segment tree, although a BBST should also work fine too.

In total, around prime numbers will be inserted/erased in the data structure (there is probably a better bound, but this is good enough). Each insert/erase operation will take time to update the product (this time complexity can be improved).

Time Complexity: , or slightly better

Note 1: If you have trouble passing this problem, you may find useful. In the best case, this optimization will cut your runtime in half.

Note 2: Some programmers have found a way to solve this problem using FFT. I may include an analysis of this approach later.