Counting Binary Trees

Time Limit: 3000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3240
64-bit integer IO format: %I64d      Java class name: Main

Description

There are 5 distinct binary trees of 3 nodes:

Let T(n) be the number of distinct non-empty binary trees of no more than n nodes, your task is to calculate T(n) mod m.

Input

The input contains at most 10 test cases. Each case contains two integers n and m (1 <= n <= 100,000, 1 <= m <= 109) on a single line. The input ends with n = m = 0.

Output

For each test case, print T(n) mod m.

Sample Input

3 100
4 10
0 0

Sample Output

8
2

Source

Language: 
Theme: 
Share Code? 

Powered by NB231 | Current Style: .