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

来源:互联网 发布:那个新闻软件最好 编辑:程序博客网 时间:2024/06/06 13:58

传送门: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 longtemplate<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 (mod104+7104+7).
SAMPLE INPUT
21 12 2
SAMPLE OUTPUT
12
SOLUTION
“玲珑杯”ACM比赛 Round #4


0 0
原创粉丝点击