BZOJ 3907: 网格
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: 网格的更多相关文章
- bzoj 3907: 网格 组合数学
3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 13 Solved: 7[Submit][Status][Discuss] Descr ...
- 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)的任意一条路径都对应( ...
- BZOJ 3907: 网格 [Catalan数 高精度]
3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 402 Solved: 180[Submit][Status][Discuss] De ...
- bzoj 3907 网格 bzoj2822 [AHOI2012]树屋阶梯——卡特兰数(阶乘高精度模板)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3907 https://www.lydsy.com/JudgeOnline/problem.p ...
- BZOJ 3907: 网格【组合数学】
Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过 ...
- 【BZOJ 3907】网格 组合数学
大家说他是卡特兰数,其实也不为过,一开始只是用卡特兰数来推这道题,一直没有怼出来,后来发现其实卡特兰数只不过是一种组合数学,我们可以退一步直接用组合数学来解决,这道题运用组合数的思想主要用到补集与几何 ...
- 【BZOJ 3907】网格(Catalan数)
题目链接 这个题推导公式跟\(Catalan\)数是一样的,可得解为\(C_{n+m}^n-C_{n+m}^{n+1}\) 然后套组合数公式\(C_n^m=\frac{n!}{m!(n-m)!}\) ...
- 【BZOJ】【3907】网格
组合数学/python 3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 162 Solved: 76[Submit][Status][ ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- 词性标注 parts of speech tagging
In corpus linguistics, part-of-speech tagging (POS tagging or POST), also called grammatical tagging ...
- MySQL增加列,修改列名、列属性,删除列
mysql修改表名,列名,列类型,添加表列,删除表列 alter table test rename test1; --修改表名 alter table test add column name v ...
- PyCharm 教程(四)显示行号
PyCharm 教程(四)显示行号 在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启Py ...
- Main函数 & Autoreleasepool
如同任何基于C的应用程序,程序启动的主入口点为iOS应用程序的main函数.在iOS应用程序,main函数的作用是很少的.它的主要工作是控制UIKit framework.因此,你在Xcode中创建任 ...
- android自定义控件(6)-详解在onMeasure()方法中如何测量一个控件尺寸
今天的任务就是详细研究一下protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法.如果只是说要重写什么方法有什么 ...
- [Angularjs]表单验证
写在前面 在开发中提交表单,并对表单的值进行验证是非常常见的操作,angularjs对表单验证提供了非常好的支持. demo 表单 <form name="myform" n ...
- 自定义列表dl的使用原因和场合
为什么要使用自定义列表? dl和ol, ul的区别? 要正确理解dl的意图, 理解 dl的 "语义" ! 才能知道为什么要使用dl, 以及在什么时候/ 什么情况下使用 dl? dl ...
- UvaLive6662 The Last Ant 模拟
UvaLive6662 PDF题目 题意:给出隧道长度L,蚂蚁数量N,各蚂蚁位置Pi.前进方向Di,都为整数(前进方向为L或R),蚂蚁速度为1cm每秒,两蚂蚁若在整数点相遇则都反向,若不在整数点相遇则 ...
- Mastering Web Application Development with AngularJS 读书笔记-前记
学习AngularJS的笔记,这个是英文版的,有些地方翻译的很随意,做的笔记不是很详细,用来自勉.觉得写下来要比看能理解的更深入点.有理解不对的地方还请前辈们纠正! 一.关于<Mastering ...
- 使用Minify来优化网站性能
Minify 是用PHP5开发的应用,通过遵循一些Yahoo的优化规则来提高网站的性能.它会合并多个CSS或者JavaScript文件,移除一些不必要的空格和注释,进行gzip压缩,并且会设置浏览器的 ...