HDU 5698 瞬间移动 (组合数 + 阶乘逆元)

来源:互联网 发布:防止sql注入攻击 编辑:程序博客网 时间:2024/05/16 19:35

瞬间移动

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 191    Accepted Submission(s): 99


Problem Description
有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几种方案,答案对1000000007取模。

Input
多组测试数据。
两个整数n,m(2n,m100000)
 
Output
一个整数表示答案
 
Sample Input
4 5
 
Sample Output
10
 
Source
2016"百度之星" - 初赛(Astar Round2B)
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5698

题目分析:因为只能往右下走,所以其实外围的一圈都没用,可以直接删掉第一行最后一行第一列最后一列,然后剩下来的子问题就很熟悉了,答案就是C(n - 2 + m - 2, n - 2),求组合数用阶乘,预处理阶乘和阶乘逆元
#include <cstdio>#include <cstring>#define ll long longint const MOD = 1e9 + 7;int const MAX = 200000;ll fac[MAX + 5], inv_fac[MAX + 5];int n, m;ll qpow(ll x, ll n){    ll res = 1;    while(n)    {        if(n & 1)            res = (res * x) % MOD;        x = (x * x) % MOD;        n >>= 1;    }    return res;}void pre(){    fac[0] = 1;    for(int i = 1; i <= MAX; i++)        fac[i] = (fac[i - 1] * i) % MOD;    inv_fac[MAX] = qpow(fac[MAX], MOD - 2);    for(int i = MAX - 1; i >= 0; i--)        inv_fac[i] = (inv_fac[i + 1] * (i + 1)) % MOD; }int main(){    pre();    while(scanf("%d %d", &n, &m) != EOF)    {        ll a = fac[n + m - 4] % MOD;        ll b = (inv_fac[n - 2] * inv_fac[m - 2]) % MOD;        printf("%I64d\n", (a * b) % MOD);    }}


0 0
原创粉丝点击