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;
}

  

时间: 2024-08-03 03:41:02

BZOJ 3907: 网格的相关文章

bzoj 3907: 网格 组合数学

3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 13  Solved: 7[Submit][Status][Discuss] Description 某 城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过图示中直线左上方的点,即任何途径的点(x, y)都要满足x >= y,请问在这些前提下,到达B(n, m)有多少

BZOJ 3907: 网格【组合数学】

Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过图示中直线左上方的点,即任何途径的点(x, y)都要满足x >= y,请问在这些前提下,到达B(n, m)有多少种走法. Input 输入文件中仅有一行,包含两个整数n和m,表示城市街区的规模. Output 输出文件中仅有一个整数和一个换行/回车符,表示不同的方案总数. Sample Input 6

【BZOJ 3907】 网格

3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MB Submit: 159  Solved: 74 [Submit][Status][Discuss] Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过图示中直线左上方的点,即任何途径的点(x, y)都要满足x >= y,请问在这些前提下,到达B(n, m)

【BZOJ】【3907】网格

组合数学/python 题面很容易想到Catalan数……但是5000的范围实在是有些吃不消…… 题解:http://www.cnblogs.com/mhy12345/p/4343980.html copy了下代码sorry…… 1 /************************************************************** 2 Problem: 3907 3 User: Tunix 4 Language: Python 5 Result: Accepted 6

[UOJ#220][BZOJ4651][Noi2016]网格

试题描述 跳蚤国王和蛐蛐国王在玩一个游戏. 他们在一个 n 行 m 列的网格上排兵布阵.其中的 c 个格子中 (0≤c≤nm),每个格子有一只蛐蛐,其余的格子中,每个格子有一只跳蚤. 我们称占据的格子有公共边的两只跳蚤是相邻的. 我们称两只跳蚤是连通的,当且仅当这两只跳蚤相邻,或存在另一只跳蚤与这两只跳蚤都连通. 现在,蛐蛐国王希望,将某些(0 个,1 个或多个)跳蚤替换成蛐蛐,使得在此之后存在至少两只跳蚤不连通. 例如:我们用图表示一只跳蚤,用图表示一只蛐蛐,那么图 1 描述了一个 n=4,m

BZOJ 题目整理

bzoj 500题纪念 总结一发题目吧,挑几道题整理一下,(方便拖板子) 1039:每条线段与前一条线段之间的长度的比例和夹角不会因平移.旋转.放缩而改变,所以将每条轨迹改为比例和夹角的序列,复制一份翻转后的序列,直接上AC自动机即可.注意特判 1125:hash+splay 1183:digit-product只可能是2,3,5,7的积,枚举digit-product进行dp即可 1301:每个点和每个边只能被删除一次,随便搞 1313:上下界最大流 1471:考虑容斥,枚举两条路径相交的位置

[BZOJ 1207]打鼹鼠

[问题描述] 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气.你可以控制一个机器人来打鼹鼠,如果i时刻鼹鼠在某个网格中出现,而机器人也处于同一网格的话,那么这个鼹鼠就会被机器人打死.而机器人每一时刻只能够移动一格或停留在原地不动.机器人的移动是指从当前所处的网格移向相邻的网格,即从坐标为(i,j)的网格移向(i-1, j),(i+1, j),(i,j-1)

【BZOJ】【1018】【SHOI2008】堵塞的交通traffic

线段树 这题的线段树+分类讨论蛮神奇的……我以前学的线段树简直就是渣渣QAQ 看了下ydc题解里的思想>_>用线段树维护连通性!那么就自己写吧……每个节点表示一段区间的连通性(我的叶子节点表示的是一个方块型的四个点之间的连通性,所以我直接n--了)对线段树上每个节点维护6个信息,即四个端点中任意一对点之间的连通性. 维护连通性的时候要进行信息的整合,也就是说间接连通的要全部找到并标记上连通,举个例子:如果左边连通且下边两端点连通,则左上到右下连通.这是一个简单的讨论我就不细说了,自己想一下真的

[BZOJ 1001](BeiJingOI 2006)狼抓兔子

Description   Source: Beijing2006 [BJOI2006] 八中OJ上本题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: 左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)