algorithm - Path of Length N in graph with constraints -


I want to find the number of paths of length n in a graph where the top can be any natural number although the two headings only Only if the product of two corners is less than some natural number P If the products of the two heads are more than P, which are not connected to them and can not be reached from each other.

I can clearly run two nested loops (& lt; = P) and make a proximity matrix, but the P can be very large and this approach can be very slow. Can anybody think of some optimal approach to solving the problem? Can we solve it using dynamic programming?

I agree with the pre-recurrence, although I have a slightly simplified version. Note that I am using letter P for the maximum product name, because it is used in:

  f (1, x) = 1f (i, x) = sum (F (i, x)  x for   

Em> is i the number of sequences of length that ends with x . The answer to the question is then f (n + 1, 1) .

Certainly P can be up to 10 ^ 9 in this work, with a simple implementation, the DP table is out of the question, however, only M & LT; Let's say the maximum segment of a j ... b j of floor (p / i) , Where floor (p / a j ) = floor (p / b j ) we O (the number of sections * Log P) can use binary search. Imagine the full DP table for

f . There are different values ​​for floor (p / x) m , so f in every line of m There are adjacent limitations which have equal value.

Then we calculate the compressed DP table, where we represent rows (length, values) as a list of pairs. We start with F (1) = [[P, 1]] and we can calculate the computing prefix amount of F (I + 1) by processing the sections in ascending order and the length stored in F (i) Are there.

The total runtime for this implementation of this approach is O (m (log P + n)) This is the code I used:

  ll = long time using; Const int mod = 1000000007; Add zero (int and x, l) {x = (x + y)% mod; } Int main () {int n, p; Cin & gt; & Gt; N & gt; & Gt; P; Int x = 1; The vector & lt; Pair & lt; Integer, integer & gt; & Gt; Regions; Whereas (x & lt; = P) {int y = x + 1, hi = P + 1; While (y & lt; en) {int mid = (y + hi) / 2; If (P / Middle Lt; P / X) Hi = Middle; Else y = mid + 1; } Section push_back (make_pair (p / x, y-x)); X = y; } Reverse (start), and (segments)); The vector & lt; Pair & lt; Integer, integer & gt; & Gt; DP; dp.push_back (make_pair (p, 1)); For (int i = 1; i & lt; = n; ++ i) {int j = 0; Int sum_smaller = 0, cnt_smaller = 0; The vector & lt; Pair & lt; Integer, integer & gt; & Gt; DP2; (For Auto: Segment) {int value = it.first, cnt = it.second; Whereas (cnt_smaller + dp [j]. First & lt; = value) {cnt_smaller + = dp [j]. First; (Sum_smaller, (ll) dp [j] first * dp [ja] .second); J ++; } Int pref_sum = sum_smaller; If (value> cnt_smaller) add (pref_sum, (ll) (value - cnt_smaller) * dp [j] .second); Dp2.push_back (make_pair (cnt, pref_sum)); } DP = DP2; Reverse (start (dp) end (dp)); } cout & lt; & Lt; DP [0] .second & lt; & Lt; Endl; }   

I need to do some subtle customization to get AC, but they are not really relevant, so I left them.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -