Given a string str of length N. The task is to find the number of integers obtained by replacing ‘?’ with any digit such that the formed integer gives remainder 5 when it is divided by 13.
Numbers can also begin with zero. The answer can be very large, so, output answer modulo 109 + 7.
Input: str = “?44”
Only possible number is 044
Input: str = “7?4”
Input: str = “8?3?4233?4?”
Approach: Let dp[i][j] be the number of ways to create an i-digit number consistent with the first i digits of the given pattern and congruent to j modulo 13. As our base case, dp[i]=0 for i from 1 to 12, and dp=1 (as our length-zero number has value zero and thus is zero mod 13.)
Notice that appending a digit k to the end of a number that’s j mod 13 gives a number that’s congruent to 10j+k mod 13. We use this fact to perform our transitions. For every state, dp[i][j] with i < N, iterate over the possible values of k. (If s[i]=’?’, there will be ten choices for k, and otherwise, there will only be one choice.) Then, we add dp[i][j] to dp[i+1][(10j+k)%13].
To get our final answer, we can simply print dp[N].
Below is the implementation of the above approach :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Find the remainder when First digit of a number is divided by its Last digit
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Count the number of ways to give ranks for N students such that same ranks are possible
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Number of distinct integers obtained by lcm(X, N)/X
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Print combinations of distinct numbers which add up to give sum N
- Check if a string can be converted to another string by replacing vowels and consonants
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be obtained by rotating another string d places
- String obtained by reversing and complementing a Binary string K times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.