bzoj1485

http://www.lydsy.com/JudgeOnline/problem.php?id=1485

卡特兰数。

把第1,3,...,2N-1个位置看做左括号,第2,4,...,2N个位置看成右括号。

考虑从1到2N把数放进去,其实就变成了括号序列。

所以是卡特兰数。

求$\frac{C_{2n}^{n}}{n+1}\%P$可以看我下一篇博客。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
  }

const int maxN=2000000;

int N;LL P;
LL ans;

inline LL power(LL a,LL k){LL x=1,y=a;while(k){if(k&1)x=x*y%P;k>>=1;y=y*y%P;}return x;}

int flag[maxN+100],cnt,prime[maxN+100];
int a[maxN+100];
LL b[maxN+100];

int main()
  {
      freopen("bzoj1485.in","r",stdin);
        freopen("bzoj1485.out","w",stdout);
        int i,j;
        cin>>N>>P;
        re(i,2,2*N)
          {
              if(!flag[i])prime[++cnt]=i;
              for(j=1;j<=cnt && i*prime[j]<=2*N;j++)
                {
                    flag[i*prime[j]]=1;
                    a[i*prime[j]]=prime[j];
                    if(i%prime[j]==0)break;
                }
          }
        re(i,2,N)b[i]=-1;
        re(i,N+2,2*N)b[i]=1;
        ans=1;
        red(i,2*N,2)
          if(!flag[i])
            ans=ans*power(LL(i),b[i])%P;
          else
            b[a[i]]+=b[i],b[i/a[i]]+=b[i];
        cout<<ans<<endl;
        return 0;
    }
View Code

 

posted @ 2015-08-07 20:19  maijing  阅读(251)  评论(0编辑  收藏  举报