Description

求拼成阶梯状的方案数.

Sol

高精度+Catalan数.

我们可以把最后一行无线延伸,所有就很容易看出Catalan数了.

\(f_n=f_0f_{n-1}+f_1f_{n-2}+f_2f_{n-3}+...+f_{n-1}f_0\)

这就是Catalan数了,高精贴板子...

Code

/**************************************************************
Problem: 2822
User: BeiYu
Language: C++
Result: Accepted
Time:20 ms
Memory:1308 kb
****************************************************************/ #include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std; typedef long long LL;
const int B = 10;
const int W = 1; struct Big{
vector<int> s;
void clear(){ s.clear(); } Big(LL num=0){ *this=num; }
Big operator = (LL x){
clear();
do{ s.push_back(x%B),x/=B; }while(x);
return *this;
}
Big operator = (const string &str){
clear();
int x,len=(str.length()-1)/W+1,l=str.length();
for(int i=0;i<len;i++){
int tt=l-i*W,st=max(0,tt-W);
sscanf(str.substr(st,tt-st).c_str(),"%d",&x);
s.push_back(x);
}return *this;
}
}; istream& operator >> (istream & in,Big &a){
string s;
if(!(in>>s)) return in;
a=s;return in;
} ostream& operator << (ostream &out,const Big &a){
cout<<a.s.back();
for(int i=a.s.size()-2;~i;i--){
cout.width(W),cout.fill('0'),cout<<a.s[i];
}return out;
} bool operator < (const Big &a,const Big &b){
int la=a.s.size(),lb=b.s.size();
if(la<lb) return 1;if(la>lb) return 0;
for(int i=la-1;~i;i--){
if(a.s[i]<b.s[i]) return 1;
if(a.s[i]>b.s[i]) return 0;
}return 0;
}
bool operator <= (const Big &a,const Big &b){ return !(b<a); }
bool operator > (const Big &a,const Big &b){ return b<a; }
bool operator >= (const Big &a,const Big &b){ return !(a<b); }
bool operator == (const Big &a,const Big &b){ return !(a>b) && !(a<b); }
bool operator != (const Big &a,const Big &b){ return a>b || a<b ; } Big operator + (const Big &a,const Big &b){
Big c;c.clear();
int lim=max(a.s.size(),b.s.size()),la=a.s.size(),lb=b.s.size(),i,g,x;
for(i=0,g=0;;i++){
if(g==0 && i>=lim) break;
x=g;if(i<la) x+=a.s[i];if(i<lb) x+=b.s[i];
c.s.push_back(x%B),g=x/B;
}i=c.s.size()-1;
while(c.s[i]==0 && i) c.s.pop_back(),i--;
return c;
}
Big operator - (const Big &a,const Big &b){
Big c;c.clear();
int i,g,x,la=a.s.size(),lb=b.s.size();
for(i=0,g=0;i<la;i++){
x=a.s[i]-g;
if(i<lb) x-=b.s[i];
if(x>=0) g=0;else g=1,x+=B;
c.s.push_back(x);
}i=c.s.size()-1;
while(c.s[i]==0 && i) c.s.pop_back(),i--;
return c;
}
Big operator * (const Big &a,const Big &b){
Big c;
int i,j,la=a.s.size(),lb=b.s.size(),lc=la+lb;
c.s.resize(lc,0);
for(i=0;i<la;i++) for(j=0;j<lb;j++) c.s[i+j]+=a.s[i]*b.s[j];
for(i=0;i<lc;i++) c.s[i+1]+=c.s[i]/B,c.s[i]%=B;
i=lc-1;while(c.s[i]==0 && i) c.s.pop_back(),i--;
return c;
}
Big operator / (const Big &a,const Big &b){
Big c,f=0;
int la=a.s.size(),i;
c.s.resize(la,0);
for(i=la-1;~i;i--){
f=f*B,f.s[0]=a.s[i];
while(f>=b) f=f-b,c.s[i]++;
}i=la-1;while(c.s[i]==0 && i) c.s.pop_back(),i--;
return c;
}
Big operator % (const Big &a,const Big &b){
Big c=a-(a/b)*b;
return c;
}
Big operator ^ (Big &a,Big &b){
Big c=1;
for(;b!=0;b=b/2,a=a*a){
if(b.s[0] & 1) c=c*a;
}return c;
}
Big operator += (Big &a,const Big &b){ return a=a+b; }
Big operator -= (Big &a,const Big &b){ return a=a-b; }
Big operator *= (Big &a,const Big &b){ return a=a*b; }
Big operator /= (Big &a,const Big &b){ return a=a/b; }
Big operator %= (Big &a,const Big &b){ return a=a%b; } const int N = 1005; int cnt;
int b[N],pr[N],minp[N],c[N]; void Pre(int t){
minp[1]=0;
for(int i=2;i<=t;i++){
if(!b[i]) pr[++cnt]=i,minp[i]=cnt;
for(int j=1;j<=cnt && i*pr[j]<=t;j++){
b[i*pr[j]]=1,minp[i*pr[j]]=j;
if(i%pr[j]==0) break;
}
}
}
void Add(int x,int v){ while(x>1) c[minp[x]]+=v,x/=pr[minp[x]]; }
int main(){
ios::sync_with_stdio(false);
int n;Big ans=1,a,b;
cin>>n;
Pre(n*2); for(int i=n+1;i<=2*n;i++) Add(i,1);
for(int i=1;i<n;i++) Add(i,-1);
Add(n,-1),Add(n+1,-1); for(int i=1;i<=cnt;i++) a=pr[i],b=c[i],ans*=a^b; cout<<ans<<endl;
return 0;
}

  

BZOJ 2822: [AHOI2012]树屋阶梯的更多相关文章

  1. BZOJ 2822: [AHOI2012]树屋阶梯 [Catalan数 高精度]

    2822: [AHOI2012]树屋阶梯 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 779  Solved: 453[Submit][Status] ...

  2. bzoj 2822 [AHOI2012]树屋阶梯 卡特兰数

    因为规定n层的阶梯只能用n块木板 那么就需要考虑,多出来的一块木板往哪里放 考虑往直角处放置新的木板 不管怎样,只有多的木板一直扩展到斜边表面,才会是合法的新状态,发现,这样之后,整个n层阶梯就被分成 ...

  3. 【BZOJ 2822】2822: [AHOI2012]树屋阶梯(卡特兰数+高精度)

    2822: [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处 ...

  4. 2822: [AHOI2012]树屋阶梯

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1161  Solved: 694[Submit][Status][Discuss] Descriptio ...

  5. bzoj2822[AHOI2012]树屋阶梯(卡特兰数)

    2822: [AHOI2012]树屋阶梯 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 879  Solved: 513[Submit][Status] ...

  6. [AHOI2012]树屋阶梯 题解(卡特兰数)

    [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营. ...

  7. 洛谷P2532 [AHOI2012]树屋阶梯(Catalan数)

    P2532 [AHOI2012]树屋阶梯 题目描述 输入输出格式 输入格式: 一个正整数N(1<=N<=500),表示阶梯的高度. 输出格式: 一个正整数,表示搭建方法的个数.(注:搭建方 ...

  8. P2532 [AHOI2012]树屋阶梯

    题目:P2532 [AHOI2012]树屋阶梯 思路: 打表之后不难看出是裸的Catalan数.简单证明一下: 对于任意一种合法方案,都可以表示为在左下角先放一个\(k*(n+1-k),k\in[1, ...

  9. 【BZOJ 2822】[AHOI2012]树屋阶梯 卡特兰数+高精

    这道题随便弄几个数就发现是卡特兰数然而为什么是呢? 我们发现我们在增加一列时,如果这一个东西(那一列)他就一格,那么就是上一次的方案数,并没有任何改变,他占满了也是,然后他要是占两格呢,就是把原来的切 ...

随机推荐

  1. python内置函数每个执行一次

      open    #   with open('log','r') as f:    或者   r=open(filename,r+) with open ('1.txt','r',encoding ...

  2. Windows下安装Tomcat服务

    startup.bat中添加以下内容 setlocal SET JAVA_HOME=D:\Program Files\Java\jdk1.8.0_05 SET CATALINA_HOME=D:\Pro ...

  3. <Web 之困 现代Web应用安全指南>一本好书 69.00?

    NET代码安全 界面漏洞防范与程序优化 一. SQL 注入攻击的源头 1. 过滤或转移危险字符 2.  使用SqlParameter类:.NET 框架有一个叫做SqlParameter 的集合类型,可 ...

  4. XMLBEANS的使用总结

    在本文中,我们将详细了解最好的数据对象XMLBean.从传统角度来说,在Java应用程序中使用XML,就是在从 XML文档向Java导入数据的技术或从数据模型层向XML导出数据技术之间架起了一座桥梁. ...

  5. VS Code 开发asp.net core 遇到的坑

    摘要 微软发布.NET Core 1.0,ASP.NET Core 1.0 与 Entity Framewok 1.0也有一段时间了,一直没进行这方面的学习,节前公司让调研这方面的可行性.所以还是从最 ...

  6. Promise 异步(asynchronous )编程

    概述 Promise.all(iterable) 方法返回一个promise,该promise会等iterable参数内的所有promise都被resolve后被resolve,或以第一个promis ...

  7. thinkphp的url地址区分大小写?

    在默认情况下: 在访问url地址的时候, 其中的 Action类名 即: 模块名称 是区分大小写的. (只有模块名, 即控制器名称) 可以根据设置 'URL_CASE_INSENSITIVE' =&g ...

  8. Java基础相关总结

    临近面试,权当复习了吧 final相关 定义常量的方法  eg:final int i=0;//则i不能被修改 final修饰的类不能被继承,因此没有子类,且它的类中的方法默认是final final ...

  9. 梳理javascript原型整体思路

    相信很多对javascript原型初步了解的人都知道prototype,constructor,__proto__这些名词,也在一定程度上可以使用这些对象.属性.甚至知道在构造函数的原型上定义方法供实 ...

  10. [译]Probable C# 6.0 features illustrated

    原文: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated ========================= ...