Bzoj1485: [hnoi2009] interesting series

Source: Internet
Author: User
1485: [hnoi2009] interesting series time limit: 10 sec memory limit: 64 MB
Submit: 623 solved: 336
[Submit] [Status] Description

It is interesting to call a 2n series. if and only when the series meets the following three conditions:

(1) It is an array of 2n integers from 1 to 2N {ai };

(2) All odd values meet A1 <A3 <... <A2n-1, all the even numbers meet A2 <A4 <... <A2n;

(3) Any adjacent two a2i-1 and a2i (1 ≤ I ≤ n) meet the odd number of items less than the even number, that is: a2i-1 <a2i.

The current task is: for a given n, the number of interesting sequences with different lengths of 2n in the request. Because the final answer may be very large, we only need to output the mod P value of the answer.

 

Input

The input file contains only two integers N and P separated by spaces. Input data guarantees that 50% of the data meet n ≤ 1000,100% of the data meet n ≤ 1000000 and p ≤ 1000000000.

 

Output

Contains only one integer, indicating the mod P value of the number of interesting series with different lengths of 2n.

 

Sample input 3 10 sample output 5


The five interesting sequences are, ).
Hint Source

 

Problem: At the beginning, YY has a DP of N ^ 2, which is as follows: because the first item must be <the last one, and an odd number <even number, we consider entering numbers in a series from small to large, then, the number of odd digits at any time must be no less than the number of even digits. f [I] [J] [k] indicates putting it to I, j odd digits and K even digits (I won't say I can scroll, K digits are useless, just to facilitate the description of 233) then f [I] [J] [k] = f [I-1] [J-1] [k] + F [I-1] [J] [k-1] J> KF [I] [j] [k] = f [I-1] [J] [k-1] J = K should be well understood, because the odd J-bit must be <even J-bit, then n ^ 2 can get this algorithm to get 50 points. Then I didn't come up with any optimization methods, then I clicked on the question to find that the number of Catalan was instantly stunned. Or the above idea, we will draw this process to the Cartesian coordinate system of the plane starting point is (0, 0) if you put a number line on the odd digit, it will take a grid above the oblique, otherwise, the end point to the lower right is (2 * n, 0). The line must always be on the top of the X axis. Isn't that a classic question about the catalan number buying movie tickets! The answer is C (2 * n, n)/(n + 1). At this point, the first part of this question is completed. If the number of modulus is a prime number, it is obvious that we can reverse the yuan, but now the number of Modulo is not a prime number. Because the answer must be an integer, we should consider decomposing each prime factor, and then remove the Division. This is O (n * logn, for million data, we should be able to use an O (n) algorithm: Convert from edevil. live sequence I: add the J power to the answer. If K is a qualitative factor of I, I only need to hand over the task to K and I/K, because I ^ J = K ^ J * (I/K) ^ J, when it is the turn to calculate K or I/K, you only need to add his index + J, if I is a prime number, simply add the answer. Because the final answer is an integer, the I index must be a positive number. I used linear screening to find a qualitative factor of a number, and then I achieved O (N). For details, refer to the Code:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 200100014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 using namespace std;23 inline int read()24 {25     int x=0,f=1;char ch=getchar();26     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}27     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}28     return x*f;29 }30 int n,tot,mod,p[maxn],v[maxn],b[maxn];31 ll ans=1;32 inline ll power(ll x,ll y)33 {34     ll t=1;35     for(;y;y>>=1,x=x*x%mod)36      if(y&1)t=t*x%mod;37     return t;38 }39 int main()40 {41     freopen("input.txt","r",stdin);42     freopen("output.txt","w",stdout);43     n=read();mod=read();44     for2(i,2,n<<1)45     {46      if(!v[i])p[++tot]=i;47      for1(j,tot)48       {49           int k=i*p[j];50           if(k>2*n)break;51           v[k]=p[j];52           if(i%p[j]==0)break;53       }54     }55     for2(i,2,n)b[i]=-1;56     for2(i,n+2,n<<1)b[i]=1;57     for3(i,n<<1,2)58     if(!v[i])ans=ans*power(i,b[i])%mod;59     else b[v[i]]+=b[i],b[i/v[i]]+=b[i];60     printf("%lld\n",ans);61     return 0;62 }
View code

 

Bzoj1485: [hnoi2009] interesting series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.