PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#QHD0000027. Đội quân Caesar (Caesars Legions)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đội quân Caesar (Caesars Legions)
Caesars Legions
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Caesar has foot soldiers and horse soldiers. Soldiers of the same type are indistinguishable, and all soldiers must be placed in one row.
A valid formation contains no more than consecutive foot soldiers and no more than consecutive horse soldiers. Every soldier must be included exactly once.
Count all distinct valid formations modulo .
Input
One line contains the integers , , , and .
Output
Print the number of valid formations modulo .
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
2 1 1 2
Output
1
Explanation
We have two foot soldiers and one horse soldier. Because , the two foot soldiers cannot be adjacent. The only valid arrangement is foot–horse–foot, and it also satisfies . Thus the answer is .