DTPOLY2 - Divide Polygon (HARD)
This is hard version of DTPOLY.
Determine the number of ways to cut a convex polygon with N vertices if the only cuts allowed are from vertex to vertex, each cut divides exactly one polygon into exactly two polygons, and you must end up with exactly K polygons. Consider each vertex distinct. For example, there are three ways to cut a square - the two diagonals and not cutting at all - but only two ways to cut it to form 2 polygons, and only one way to cut it to form 1 polygon. The order of cuts does not matter. Since the number of ways can be very large, you should return the number taken modulo M.
Input
Input contains several test cases, i-th line consists of 3 integers: Ni (3 ≤ Ni, ΣNi ≤ 108 over all test cases),
Ki (1 ≤ Ki ≤ Ni - 2) and Mi (1 < Mi < 260), all pairs (Ni, Ki) are different.
Output
On the i-th line print the number of different ways to cut the polygon with Ni vertices into Ki pieces modulo Mi.
Example
Input: 4 2 100 6 3 100 10000000 2 1000000007 10000000 5000000 1000000014000000049 Output: 2 21 984650007 780127215155143528
hide comments
[Lakshman]:
2016-06-01 10:36:46
Can some one help me. What complexity we need to get AC for the hard version. Mine O(N * log num) where (num) is the largest number to be factored.
|
|
Francky:
2014-09-04 18:32:59
Yeah!!! AC at first attempt, I could pass the 10(?) test files without too much optimizations, now is time for opti. The problem is very nice and I don't know problems here with a similar way to the green AC. Thanks a lot.
|
|
[Rampage] Blue.Mary:
2014-09-04 18:32:59
1. You may create a new problem rather than simply change the problem description.
|
|
Francky:
2014-09-04 18:32:59
@ Michael K.: You just have to accord the example with some moduli. <fixed>
|
Added by: | Michael Kharitonov |
Date: | 2013-03-01 |
Time limit: | 6s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |