传送门: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;
}
描述:
Time Limit:1s Memory Limit:64MByte
Submissions:316Solved:78
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)(x≥y)(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),(i≥j)(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.