Let P(x) be a function that returns the product of digits of x. For example P(935) = 9*3*5 = 135. Now let us define another function P_repeat(x):
int P_repeat(int x){
if(x < 10) return x
return P_repeat(P(x))
}
For a value v, find the number of numbers x less than N such that P_repeat(x) = v.
How would I make a transition between states in a digit DP for this problem?
Please help, and thanks in advance!
UPD: The bounds are N <= 10^13.
First notice that the product of digits of x is always less than x, so that you actually have a DAG. Now the graph is defined by a single relation x->P(x), so we, in fact, have a tree (well, a forest). Why don't you explicitly build the forest for whatever your range is (I'm assuming it's small, because you asked about DP)? Maybe then you can visualize it easier.
For example, if it's a single query (N,v) it's enough to run a dfs from v. If you need a lot of queries and MAX_x < 10^5 or so then you can use sack ("dsu on tree") with an order statistic set on each node so each node knows all of its children and how many are less than N. And so on.
See above for the bounds. Sorry about not posting them earlier.
Oh, that's very different than what I had in mind then. It looks tricky, do you have a link? We can tell right away that most numbers satisfy P_repeat(x) = 0 (96% of all numbers in [0,1e8], 94% in [0,1e7]) but that doesn't seem enough to force enumerating them for all v != 0 to work.
You said that n ≤ 1013, though the function in the code is 32bit. Do you think about the overflow or mod 232?
P.S. I think you should write "long long" or "long", not "int".
The int is meant as a placeholder, to show that the value passed is an integer. The mod 2^32 and overflow is overcomplicating matters.
Auto comment: topic has been updated by vamaddur (previous revision, new revision, compare).
You can sort the digits first and the answer doesn't change. The number of the possible sorted sequence of digits is not too big so we can enumerate them all.
@anta I agree with you, but I am still interested in seeing a Digit DP solution, as that is what I am trying to practice.
Consider this pseudo code of a digit DP:
rec(i, prod)
counts number of integer x < 10i + 1 such thatP_repeat(prod * P(x))
is equal tov
(this is wrong if x is less than 10^i but I hope you can understand).The point is this function can be memorized with a hash map or similar to that. Because of the above argument, we can be sure that the number of states is not too big.
Can you modify this to consider only integers less than N? This part is the regular routine of the digit DP technique so you can consult other articles.
@anta I was able to count combinations of digits with length less than N.length() as shown below:
(Note that maxLen = N.length()-1, freq stores the frequency of each digit in the current permutation, and choose[i][j] denotes iCj.)
How can I modify this to count digits of the same length, but less than N?
I don't understand your question. Also, does your code actually work? It seems like broken for me.
Can you implement my pseudo code above?
It is easy to understand that two numbers with the same digits (not necessarily with the same order, for example 1244, 4214) have the same answer. There are 10^13 different nums, but there are only about 700000 different sets of digits (you can calc it using this) So we can find all different sets. How to count numbers we can create from a certain set, that are <= N? Assume function f for set S (it contains pair of {digit, its count in number}) which returns count of different numbers we can create from S (not necessarily <= N), something like this:
It's easy to find count of numbers we can create from S, which length is less than length of N, but how we can solve case with equal length? We'll go with i for digits of N, current digit is a[i]. 1) We need to put digit x < a[i], now result number is always less than N, so we can decrease count of x and add to result f(new set), then increase count of x to get back (we need to do this with all x < a[i] we have). 2) If we have a[i] in our set, we need to put it (and decrease its count) and continue, otherwise break. Then add to ans[p_repeat(product of all digits in S)] our result. We need to make it with all different sets, and then print ans[v].
I have a solution without dynamic programming (though, I don't have solution with dynamic programming).
Let f(x) = Prepeat(x) (because the function name is too long), f(x) = f(P(x)) = f(P(P(x))) = ... = P(P(P(... P(x)... ))).
The main idea is, f(a) = f(b), if b is a non-leading-zero integer that scrambled a, because obviously P(a) = P(b), so f(a) = f(P(a)) = f(P(b)) = f(b). So, you can brute-force the number of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 digits.
Let's consider counting the number of x that f(x) = v, in range of 10b ≤ x ≤ n. Let ci the number of i's (0 ≤ i ≤ 9) in x, it satisfies 0 ≤ ci, and c0 + c1 + c2 + ... + c9 = b + 1. The way is C(b + 10, 9). If b = 12 (maximum), there are only 497420 ways.
Let f(c0, c1, ..., c9) the value of f(x) if x has c0 0's, c1 1's, c2 2's,... . If f(c0, c1, ..., c9) = v, you can add "the number of integers less or equal to n, which has c0 0's, c1 1's, c2 2's,..." to the answer.
And how can calculate "the number of integers less or equal to n, which has c0 0's, c1 1's, c2 2's,..."? Let's consider about the problem.
In this case, if the first digit is 1, it certainly satisfies the condition, and there are 12 ways (because you can calculate the permutation of "1223"). if the first digit is 2 and second digit is 1, it certainly satisfies the condition, and there are 6 ways (because you can calculate the permutation of "123). If you repeat this, you can get the answer 12 + 6 + 1 + 1 = 20.
You can calculate the value in O(b2). Pay attention to the leading-zero values (you can't count it).
The total time complexity is . In the maximum pattern the number of calculation will be about 2 × 108, so you can calculate in time.