(0,0)->(n,m)方案数为C(n,n+m), 然后减去不合法的方案. 作(n,m)关于y=x+1的对称点(m-1,n+1), 则(0,0)->(m-1,n+1)的任意一条路径都对应(0,0)->(n,m)的一条不合法路径(y>x). 所以答案就是C(n,n+m) - C(n+1,n+m).高精度算就OK了

-----------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 10009;
const int maxpn = 1500;
 
int N, M;
int p[maxpn], pn = 0;
int a[2][maxpn];
bool check[maxn];
 
void init() {
scanf("%d%d", &N, &M);
memset(check, 0, sizeof check);
for(int i = 2, lim = N + M; i <= lim; i++) {
if(!check[i]) p[pn++] = i;
for(int j = 0; j < pn && i * p[j] <= lim; j++) {
check[i * p[j]] = true;
if(i % p[j] == 0) break;
}
}
memset(a, 0, sizeof a);
for(int i = N + 2; i <= N + M; i++)
for(int j = 0, t = i; j < pn && t >= p[j]; j++)
for(; t % p[j] == 0; a[0][j]++, a[1][j]++, t /= p[j]);
for(int t = N + 1, j = 0; j < pn && t >= p[j]; j++)
for(; t % p[j] == 0; a[0][j]++, t /= p[j]);
for(int i = 2; i < M; i++)
for(int j = 0, t = i; j < pn && t >= p[j]; j++)
for(; t % p[j] == 0; a[0][j]--, a[1][j]--, t /= p[j]);
for(int t = M, j = 0; j < pn && t >= p[j]; j++)
for(; t % p[j] == 0; a[0][j]--, t /= p[j]);
}
 
struct Int {
static const int base = 10000;
static const int maxn = 1000;
static const int width = 4;
int s[maxn], n;
Int() : n(0) {
memset(s, 0, sizeof s);
}
Int operator = (int num) {
n = 0;
for(; num; num /= base) s[n++] = num % base;
return *this;
}
Int(int x) {
*this = x;
}
Int operator = (const Int &o) {
n = o.n;
memcpy(s, o.s, sizeof(int) * n);
return *this;
}
void write() {
int buf[width];
for(int i = n; i--; ) {
int t = s[i], c = 0;
for(; t; t /= 10) buf[c++] = t % 10;
if(i + 1 != n)
for(int j = width - c; j; j--) putchar('0');
while(c--) putchar(buf[c] + '0');
}
puts("");
}
Int operator + (const Int &o) const {
Int res;
for(int i = 0, g = 0; ; i++) {
if(!g && i >= o.n && i >= n) break;
int x = g + (i < n ? s[n] : 0) + (i < o.n ? o.s[o.n] : 0);
res.s[res.n++] += x / base;
g = x % base;
}
return res;
}
Int operator += (const Int &o) {
return (*this = *this + o);
}
Int operator * (const Int &o) const {
Int ret; ret.n = o.n + n - 1;
for(int i = 0; i < n; i++)
for(int j = 0; j < o.n; j++) {
int t = s[i] * o.s[j];
ret.s[i + j] += t % base;
ret.s[i + j + 1] += t / base;
}
for(int i = 0; i < ret.n; i++) if(ret.s[i] >= base) {
ret.s[i + 1] += ret.s[i] / base;
ret.s[i] %= base;
}
for(int &i = ret.n; ret.s[i]; i++) {
ret.s[i + 1] += ret.s[i] / base;
ret.s[i] %= base;
}
return ret;
}
Int operator *= (const Int &o) {
return (*this = *this * o);
}
Int operator - (const Int &o) const {
Int ret = *this;
for(int i = ret.n; --i; ) {
ret.s[i]--; ret.s[i - 1] += base;
}
for(int i = 0; i < o.n; i++)
if((ret.s[i] -= o.s[i]) >= base)
ret.s[i + 1]++, ret.s[i] -= base;
if(!ret.s[n - 1]) ret.n--;
return ret;
}
};
 
Int power(int p, int a) {
Int ret = 1, _p = p;
for(; a; _p *= _p, a >>= 1) if(a & 1) ret *= _p;
return ret;
}
 
int main() {
init();
Int ans = 1, t = 1;
for(int i = 0; i < pn; i++)
ans *= power(p[i], a[0][i]), t *= power(p[i], a[1][i]);
(ans - t).write();
return 0;
}

-----------------------------------------------------------------

3907: 网格

Time Limit: 1 Sec  Memory Limit: 256 MB
Submit: 251  Solved: 121
[Submit][Status][Discuss]

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 6

Sample Output

132

HINT

100%的数据中,1 <= m <= n <= 5 000

Source

BZOJ 3907: 网格( 组合数 + 高精度 )的更多相关文章

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

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

  2. bzoj 3907: 网格 组合数学

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

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

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

  4. BZOJ 3907: 网格

    Description 求不跨过直线 \(y=x\) ,到达 \((n,m)\) 的方案数. Sol 组合数学+高精度. 这个推导过程跟 \(Catalan\) 数是一样的. 答案就是 \(C^{n+ ...

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

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

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

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

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

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

  8. bzoj 1002 [FJOI2007]轮状病毒 高精度&&找规律&&基尔霍夫矩阵

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2234  Solved: 1227[Submit][Statu ...

  9. bzoj 4870: [Shoi2017]组合数问题 [矩阵乘法优化dp]

    4870: [Shoi2017]组合数问题 题意:求 \[ \sum_{i=0}^{n-1} \binom{nk}{ik+r} \mod p \] \(n \le 10^9, 0\le r < ...

随机推荐

  1. SMTP邮件传输协议发送邮件和附件

    在以前接触的项目中,一直都是在做网站时用到了发送mail 的功能,在asp 和.net 中都有相关的发送mail 的类, 实现起来非常简单.最近这段时间因工作需要在C++ 中使用发送mail 的功能, ...

  2. 《Orange'S:一个操作系统的实现》笔记(一)

    感觉自己对于操作系统始终没有一个清楚的概念,尤其最近困扰于实模式.保护模式以及寻址方式等一些概念.转而一想,所有的程序,最终都是操作的计算机资源,需要和操作系统打交道,所以操作系统有必要深入了解一下. ...

  3. angular的$q服务和promise模式

    此承诺/延迟(promise/deferred)实现的灵感来自于 Kris Kowal's Q CommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action) ...

  4. Tomcat远程调试和加入JMS(转)

    1.Tomcat 加入远程调试,在catalina.bat : SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xr ...

  5. 315M无线发射模块天线的长度计算

    波长=光速/频率=300/315=0.952米 1/4波长须要的天线长度=波长*1/4=0.952/4=0.238米 考虑导线传播高频信号的缩短率在0.98左右,因此天线长度=0.238*0.98=0 ...

  6. python wsgi

    什么是wsgi? wsgi是一个web组件的接口防范,wsgi将web组件分为三类:web服务器,web中间件,web应用程序 wsgi基本处理模式为:wsgi Server -> wsgi m ...

  7. Objective-c 中的变量

    OC中的语言变量,按作用域可分为两种:局部变量和全局变量. 局部变量:也称为内部变量,局部变量是在方法内部声明的.其作用域仅限于方法内,离开该方法再使用这个变量就是非法的. 全局变量:也称为外部变量, ...

  8. Android应用开发实例篇(1)-----简易涂鸦板

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378328.html 一.概述 这次要做一个简单的涂鸦板应用,以前在Qt上实现过,突然想 ...

  9. xar安装使用方法

    xar是一种扩展的归档格式(eXtensible ARchive format),是一种开源的文件格式.xar文件在Mac OS X 10.5里是用于软件安装程序. ---------------- ...

  10. ASCII码图

    图片转ASCII码图   效果图 基本思路 把图片每个像素点的信息拿出来,最重要的是拿到rgb的值 把每个像素点由rgb转成灰度图像,即0-255 给0-255分级,把每个等级的像素点转换成ascii ...