How many integer sequences A1, A2,…, AN of length N satisfy all of the following conditions? 0≤Ai≤9 There exists some i such that Ai=0 holds. The answer can be very large, so output it modulo 10^9+7.
Explain this question please, and what should I learn to solve such types of questions?. Thank YOU
It's combinatorics based question. You need to use venn diagram to visualize it, basically there are 10**n (0,...9) ways to fill the array till N but there will be some cases where you need to fulfill the requirements i.e there will be some permutations which do not include "0" or "9", try to understand that case by simple venn diagram. the answer will be 10**n(total permutations) — 9**n(where there is no 9) — 9**n (where there is no 0) + 8**n (the intersection of prev. two cases);
10 * 10 * 10 ...up to n times
i.e.10^n.
( ^ stands for power). So this is basically the universal set containing all possible sequences. Hence, it is trivial that we need to discard certain sequences from this universal set in order to reach the desired answer.Now let's see how can we evaluate this discarded quantity:
9 * 9 * 9...upto n times , i.e. 9^n.
Since we have two cases, the total number of such sequences =2*(9^n)
.10^n - 2*(9^n)
However, we have made a slight mistake, we have doubly counted the case where both 0 and 9 are not present in the sequence (i.e the intersection set of the aforementioned two cases). So we need to add that set once back to the result. Now here we are discarding both digits 0 and 9 so we are left with 8 digits. Following what we have done so far, it is easy to see that this set has cardinality =
8^n.
10^n - 2*(9^n) + 8^n
, and by the problem, we need to provide the answer modulo1e9+7
so we do so accordingly.And for the latter part of your post, this problem was based on what is known as Principle of Inclusion and Exclusion.
Hope that helps :)