F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Rikka with Parenthesis

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 116    Accepted Submission(s): 23


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".

For a parentheses sequence, Rikka can make some operations on it. Each time she can choose two indices L and R such that L <= R. The operation modifies the characters on indices from L to R, inclusive. First, the order of these characters is reversed. Then, each character is toggled to the opposite one. That is, each '(' in the specified range changes to a ')' and vice versa.

The value of a parentheses sequence is the minimal number of the operations to change it to a correct parentheses sequence. If it is impossible, the value of the sequence is equal to 10^100.

For example, The value of '()((' is 1, the value of '()()' is 0 and the value of '(((' is 10^100.

Now Yuta shows two numbers n and m, For each K in [0,n], he wants to know the number of the different parentheses sequence of length n which has value K. The answer may be very large, he only wants to know the answer module m.

It is too difficult for Rikka. Can you help her?
 

Input
The first line contains a number t(1<=t<=10), the number of the testcases. For each testcase, the first line contains two integers n,m (1<=n<=10^6,1<=m<=10^9).
 

Output
For each testcase, print a line contains a single number. Let A[i] be the answer when K=i, you need to output $(\sum_{i=0}^n (i+1) \times A[i]) \mod m$.
 

Sample Input
1 10 100
 

Sample Output
68
 

Author
ѧ¾üÖÐѧ
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 11:16:04, Gzip enabled