Interestingly, the number of columns LOJ 10239

Interestingly, the number of columns LOJ 10239

First of all can be seen as odd stack, depending on the even stack to make, it is the number of Cattleya, in fact, play table can also be seen, but it seems you can use dp?

But the difficulty of solving the problem is not here, p is not a prime number, so do not use inversion yuan, but the top 50% of the points can be used to get Pascal's Triangle + standard, after the use of sub-prime factor decomposition is necessary.

Cattleya Numbers by formula: $ h [n] = \ frac {C_ {2n} ^ n} {n + 1} After $, simplifying its decomposition, and no decomposition start I according to the quality factor, the result T , decomposition of the quality factor to be a little faster.

 1 void add(int x,int nu)
 2 {
 3     for(int i=1;prime[i]*prime[i]<=x;i++)
 4     while(x%prime[i]==0)
 5     {
 6         cnt[prime[i]]+=nu;
 7         x/=prime[i];
 8     }
 9     cnt[x]+=nu;
10 }
11 for(int i=n+2;i<=2*n;i++)add(i,1);
12     for(int i=2;i<=n;i++)add(i,-1);
13     LL ans=1;
14     for(int i=2;i<=2*n;i++)
15         for(int j=1;j<=cnt[i];j++)
16             ans=ans*i%p;
Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #define LL long long
 4 //#define int LL
 5 using namespace std;
 6 int n,p;
 7 int cnt[2000010];
 8 int prime[20010],num;
 9 bool isprime[20010];
10 #define N 20000
11 void s()
12 {
13     for(int i=2;i<=N;i++)isprime[i]=1;
14     for(int i=2;i<=N;i++)
15     {
16         if(isprime[i])prime[++num]=i;
17         for(int j=1;j<=num&&i*prime[j]<=N;j++)
18         {
19             isprime[i*prime[j]]=0;
20             if(!i%prime[j])break;
21         }
22     }
23 }
24 void add(int x,int nu)
25 {
26     for(int i=1;prime[i]*prime[i]<=x;i++)
27     while(x%prime[i]==0)
28     {
29         cnt[prime[i]]+=nu;
30         x/=prime[i];
31     }
32     cnt[x]+=nu;
33 }
34 signed main()
35 {
36     s();
37     cin>>n>>p;
38     for(int i=n+2;i<=2*n;i++)add(i,1);
39     for(int i=2;i<=n;i++)add(i,-1);
40     LL ans=1;
41     for(int i=2;i<=2*n;i++)
42         for(int j=1;j<=cnt[i];j++)
43             ans=ans*i%p;
44     cout<<ans<<endl;
45 }
The complete code

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11222893.html