Description

求不跨过直线 \(y=x\) ,到达 \((n,m)\) 的方案数.

Sol

组合数学+高精度.

这个推导过程跟 \(Catalan\) 数是一样的.

答案就是 \(C^{n+m}_n-C^{n+m}_{n+1}\) 自己随便化简一下就是 \(\frac {(n+m)!(n-m+1)} {(n+1)!m!}\) .

然后需要先分解下质因数,再用高精度.

Code

/**************************************************************
Problem: 3907
User: BeiYu
Language: C++
Result: Accepted
Time:108 ms
Memory:1580 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;
// clear();reverse(str.begin(),str.end());
// int x,len=(str.length()-1)/W+1,l=str.length();
// for(int i=0;i<len;i++){
// int st=i,tt=min(i+W,l);
// sscanf(str.substr(st,tt-st).c_str(),"%d",&x);
// s.push_back(x);
// }return *this;
}
// Big operator = (char *str){
// clear();reverse(str.begin(),str.end());
// int x,len=(str.length()-1)/W+1,l=str.length();
// for(int i=0;i<len;i+=W){
// int s=i,t=min(i+W,l);
// sscanf(str(s,t-s),"%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 = 10005; 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);
// cout<<"qwq"<<endl;
int n,m;Big a=1,b,d;
cin>>n>>m;
Pre(n+m);
// cout<<"qwq"<<endl;
// cout<<cnt<<endl;
// for(int i=1;i<=cnt;i++) cout<<pr[i]<<" ";cout<<endl; for(int i=n+2;i<=n+m;i++) Add(i,1);
Add(n-m+1,1);
for(int i=1;i<=m;i++) Add(i,-1); for(int i=1;i<=cnt;i++){
b=pr[i],d=c[i],a*=b^d;
// cout<<b;cout<<" ";cout<<d;cout<<" ";cout<<(b^d);cout<<endl;
}
cout<<a<<endl;
return 0;
}

  

BZOJ 3907: 网格的更多相关文章

  1. bzoj 3907: 网格 组合数学

    3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 13  Solved: 7[Submit][Status][Discuss] Descr ...

  2. BZOJ 3907: 网格( 组合数 + 高精度 )

    (0,0)->(n,m)方案数为C(n,n+m), 然后减去不合法的方案. 作(n,m)关于y=x+1的对称点(m-1,n+1), 则(0,0)->(m-1,n+1)的任意一条路径都对应( ...

  3. BZOJ 3907: 网格 [Catalan数 高精度]

    3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 402  Solved: 180[Submit][Status][Discuss] De ...

  4. bzoj 3907 网格 bzoj2822 [AHOI2012]树屋阶梯——卡特兰数(阶乘高精度模板)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3907 https://www.lydsy.com/JudgeOnline/problem.p ...

  5. BZOJ 3907: 网格【组合数学】

    Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过 ...

  6. 【BZOJ 3907】网格 组合数学

    大家说他是卡特兰数,其实也不为过,一开始只是用卡特兰数来推这道题,一直没有怼出来,后来发现其实卡特兰数只不过是一种组合数学,我们可以退一步直接用组合数学来解决,这道题运用组合数的思想主要用到补集与几何 ...

  7. 【BZOJ 3907】网格(Catalan数)

    题目链接 这个题推导公式跟\(Catalan\)数是一样的,可得解为\(C_{n+m}^n-C_{n+m}^{n+1}\) 然后套组合数公式\(C_n^m=\frac{n!}{m!(n-m)!}\) ...

  8. 【BZOJ】【3907】网格

    组合数学/python 3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 162  Solved: 76[Submit][Status][ ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. JAVA 5.17习题

    1.编写并测试一个代表地址的Address类,地址信息由国家.省份.城市.街道.邮编组成,并可以返回完整的地址信息. //======================================= ...

  2. CentOS 7 AMD64安装nginx和mysql

    NGINX: rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm 查看: ...

  3. EF批量插入 扩展

    https://efbulkinsert.codeplex.com/ https://github.com/loresoft/EntityFramework.Extended

  4. Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关

    Form表单中的action路径问题,form表单action路径 热度5 评论 50 www.BkJia.Com  网友分享于:  2014-08-14 08:08:01     浏览数44525次 ...

  5. Spring--Spring容器

    在使用Spring所提供的各种丰富而神奇的功能之前,必须要在Spring IoC容器中装配好Bean,并建立Bean和Bean之间的关联关系. Spring提供了多种配置方式来实现Bean的装配.但在 ...

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

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

  7. Mysql分表和分区的区别、分库分表介绍与区别

    分表和分区的区别: 一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看:mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这 ...

  8. [Exchange]使用EWS托管API2.0同步邮箱

    你可以通过Exchange Web Serivice(EWS)托管API去检索从一个给定的时间点,文件夹中有变化的列表中的项. 客户端可以使用SyncFoldersItems方法,同步服务端的项目,你 ...

  9. 从零学习storm(一) 环境的安装配置

    1.首先 安装zookeeper   2.安装Java环境   3.安装Python   下载python包,编译安装     1.解压      2.configure     3.make     ...

  10. fedora 23如何实现 让root用户自动登录?

    没想到很简单: 只是修改一个文件的一个地方: 修改: /etc/gdm/custom.conf文件, 将自动登录 启用为true, 然后自动登录的名字设为root 即可: