HDU 5698 瞬间移动

Problem Description

有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几种方案,答案对1000000007取模。
http://acm.hdu.edu.cn/data/images/C702-1003-1.jpg

Input

多组测试数据。
两个整数n,m(2≤n,m≤100000)

Output

一个整数表示答案

Sample Input

4 5

Sample Output

10

解题思路

组合数+逆元

AC代码

#include 
#include 
#include 
#include 
#include 
using namespace std;

#define mod 1000000007

long long arr[200005];

void init() {
    arr[0] = 1;
    for (int i = 1; i < 200001; ++i) {
        arr[i] = arr[i - 1] * i % mod;
    }
}

long long func(long long a, long long b, long long c) {
    long long ret = 1;
    while (b) {
        if (b & 1) {
            ret = ret * a % c;
        }
        a = a * a % c;
        b = b >> 1;
    }
    return ret;
}

long long work(long long a, long long b) {
    return arr[a] * func(arr[a - b] * arr[b] % mod, mod - 2, mod) % mod;
}

int main() {
    init();
    int n, m;
    while (scanf("%d%d", &n, &m) != EOF) {
        printf("%lld\n", work(n - 2 + m - 2, n - 2));
    }
    return 0;
}

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5698

你可能感兴趣的:(ACM)