#SRD0000128. Đi ngược trên dải ô (Up the Strip (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đi ngược trên dải ô (Up the Strip (Hard Version))
Up the Strip (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There is a vertical strip of cells numbered through from top to bottom. A token starts at cell and must reach cell .
At cell , choose exactly one operation:
- Subtraction: choose and move to .
- Floored division: choose and move to .
Count all sequences of choices ending at cell , modulo . Different operation choices count as different ways even if they lead to the same destination.
Input
One line with two integers .
Output
Print the answer modulo .
Subtasks
In all subtasks, and is prime.
- Subtask 1 (10 points): .
- Subtask 2 (20 points): .
- Subtask 3 (30 points): .
- Subtask 4 (40 points): (full original limits).
Examples
Example 1
Input
3 998244353
Output
5
Explanation: There are three direct choices from cell to (subtract , divide by or ), and two choices via cell .
Example 2
Input
5 998244353
Output
25
Explanation: If counts paths from to , then , , , and .
Example 3
Input
42 998244353
Output
793019428
Explanation: Applying the same recurrence and reducing modulo yields .
Example 4
Input
787788 100000007
Output
94810539
Explanation: The recurrence evaluated modulo yields .