标签:ace span += 序列 ons void pac ret typedef
# 题意
1~n 分别进栈,出栈序列可能有多少种
# 题解
进出栈序列即catalan数
C(n,2n)/n+1
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 typedef long long LL; 7 const int N=120010; 8 LL primes[N],cnt,sum[N]; 9 LL ans[N],tt; 10 bool st[N]; 11 LL n; 12 void get_primes(LL n){ 13 for (int i = 2; i <=n ; ++i) { 14 if(!st[i]) primes[cnt++]=i; 15 for (int j = 0; primes[j]<=n/i ; ++j) { 16 st[primes[j]*i]=true; 17 if(i%primes[j]==0) break; 18 } 19 } 20 } 21 int get(LL n,LL p){ 22 int res=0; 23 while(n){ 24 res+=n/p; 25 n/=p; 26 } 27 return res; 28 } 29 void multi(int b) 30 { 31 LL t = 0; 32 for (int i = 0; i <= tt; i ++ ) 33 { 34 ans[i] = ans[i] * b + t; 35 t = ans[i] / 1000000000; 36 ans[i] %= 1000000000; 37 } 38 while (t) 39 { 40 ans[++tt] = t % 1000000000; 41 t /= 1000000000; 42 } 43 } 44 void out() 45 { 46 printf("%lld", ans[tt]); 47 for (int i = tt - 1; i >= 0; i -- ) printf("%09lld", ans[i]); 48 cout << endl; 49 } 50 int main(){ 51 scanf("%lld",&n); 52 get_primes(2*n); 53 54 for (int i = 0; i <cnt ; ++i) { 55 LL p=primes[i]; 56 sum[i]=get(2*n,p)-get(n,p)-get(n+1,p); 57 } 58 ans[0]=1; 59 for (int i = 0; i <cnt ; ++i) 60 for (int j = 1; j <=sum[i] ; ++j) 61 multi(primes[i]); 62 63 out(); 64 }
标签:ace span += 序列 ons void pac ret typedef
原文地址:https://www.cnblogs.com/hhyx/p/12495662.html