【玲珑杯 1049】【卡特兰数+(lucas定理+预处理)】Deg-route【求从 (0,0)到 (n,n)不穿过对角线 x = y 的方法数】

传送门:http://www.ifrog.cc/acm/problem/1049

题意:求从 (0,0)到 (n,n)不穿过对角线 x = y 的方法数


思路:

在一个格点阵列中,从 (0,0) 点走到 (n,m) 点且不经过对角线 x = y 的方法数 (x > y): C(n + m−1,m)−C(n + m−1,m−1)。
在一个格点阵列中,从 (0,0) 点走到 (n,m) 点且不穿过对角线 x = y 的方法数 (x ≥ y): C(n + m,m)−C(n + m,m−1)。


代码:

#include <iostream>
#include <stdio.h> 
#include <algorithm>
#include <string.h>
using  namespace  std;
#define ll long long

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

const int mod=1e4+7;
ll fac[mod],inv[mod];
void Pretreatment(){//预处理
    int i;
    for(fac[0]=1,i=1;i<mod;i++)
        fac[i]=fac[i-1]*i%mod;
    for(inv[1]=1,i=2;i<mod;i++)
        inv[i]=(mod-mod/i)*inv[mod%i]%mod; //逆元打表公式
    for(inv[0]=1,i=1;i<mod;i++) //用到的逆元很多,最好先打表
        (inv[i]*=inv[i-1])%=mod; //前缀逆元积
}

ll C(int n,int m){
    if(n<m) return 0;
    if(n<mod && m<mod)
        return fac[n] * inv[m] % mod * inv[n-m] % mod ;
    return C(n/mod,m/mod) * C(n%mod,m%mod) % mod ;
}

int  main(){
  int T;
  read(T);
  Pretreatment();
  while(T--){
    int x, y;
    read(x), read(y);
    ll ans = (C(x+y, y) + mod - C(x+y, y-1)) % mod; //记得加mod不然可能为负数
    printf("%lld\n", ans);
  }
  return 0;
}
描述:

1049 - Deg-route

Time Limit:1s Memory Limit:64MByte

Submissions:316Solved:78

DESCRIPTION

You are in a integer coordinate system xoyxoy, standing at point (0,0)(0,0).
A Deg-route from (0,0)(0,0) to (x,y)(xy)(x,y)(x≥y) is that you can just walk from one point to another point in up direction or right direction for just one unit length. You can just via point(i,j),(ij)(i,j),(i≥j).It means you must walk below the line y=xy=x inclusively.

How many different Deg-route from (0,0)(0,0) to (x,y)(x,y)
The answer is too big, so mod 104+7104+7.

INPUT
There are multiple test cases.The first line is a number T ( T 1000T ≤1000), which means the number of cases.For each case, two integer x,y0yx109x,y(0≤y≤x≤109).
OUTPUT
one line --- print the number of Deg-routes (mod 104+7104+7).
SAMPLE INPUT
21 12 2
SAMPLE OUTPUT
12


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值