#P3228. [HNOI2013] 数列

    ID: 2277 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>搜索2013湖南概率论,统计队列

[HNOI2013] 数列

Description

Xiao T is learning to buy stocks. He got insider information: the stock of company F will soar. The price each day is a positive integer, and due to objective reasons, it can be at most NN. During the KK days of soaring, Xiao T observed that: except for the first day, each day's price is higher than the previous day, and the increase (i.e., today's price minus yesterday's price) does not exceed MM, where MM is a positive integer. Moreover, these parameters satisfy M(K1)<NM(K-1) < N. Xiao T forgot the exact price on each of the KK days; he now wants to know how many possible price sequences there are for these KK days.

Input Format

Only one line with four space-separated numbers: NN, KK, MM, PP. For PP, see the explanation in the “Output Format” section below. It is guaranteed that for 20% of the testdata M,N,K,P20000M, N, K, P \le 20000, and for 100% of the testdata M,K,P109M, K, P \le 10^9, N1018N \le 10^{18}.

Output Format

Output a single number: the number of possible price sequences over KK days modulo PP.

7 3 2 997

16

Hint

Sample Explanation
The output sample’s 1616 means there are 1616 possible price sequences for the input sample:

{1,2,3}\{1, 2, 3\}, {1,2,4}\{1, 2, 4\}, {1,3,4}\{1, 3, 4\}, {1,3,5}\{1, 3, 5\}, {2,3,4}\{2, 3, 4\}, {2,3,5}\{2, 3, 5\}, {2,4,5}\{2, 4, 5\}, {2,4,6}\{2, 4, 6\}, {3,4,5}\{3, 4, 5\}, {3,4,6}\{3, 4, 6\}, {3,5,6}\{3, 5, 6\}, {3,5,7}\{3, 5, 7\}, {4,5,6}\{4, 5, 6\}, {4,5,7}\{4, 5, 7\}, {4,6,7}\{4, 6, 7\}, {5,6,7}\{5, 6, 7\}.

Translated by ChatGPT 5