题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5698
瞬间移动
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 639 Accepted Submission(s): 363
Problem Description
有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第
n
行第
m
列的格子有几种方案,答案对
1000000007
取模。
![](https://i-blog.csdnimg.cn/blog_migrate/6be27c7c8f0609e142553195f49b804c.jpeg)
Input
多组测试数据。
两个整数 n,m(2≤n,m≤100000)
两个整数 n,m(2≤n,m≤100000)
Output
一个整数表示答案
Sample Input
4 5
Sample Output
10
Source
Recommend
wange2014
思路:找规律,Lucas定理。将几个比较小的结果写出来,可以发现其实就是杨辉三角旋转了一下,故只要转换一下行列就可以了,行数可以将行列坐标加起来观察,列可以观察列坐标。又因为数据非常大,所以不能直接求杨辉三角。对于大组合数取模,可以用到Lucas定理。故题目转换为利用Lucas定理求C(n+m-4, m-2)%10e9+7。详见代码。
附上AC代码:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int n, m;
ll qpow(ll a, ll k, ll p){
ll ans = 1;
while (k){
if (k & 1)
ans = (ans * a) % p;
a = (a * a) % p;
k >>= 1;
}
return ans;
}
ll comb(ll a, ll b, ll p){
if (a < b)
return 0;
if (b > a-b)
b = a-b;
ll up=1, down=1;
for (ll i=0; i<b; ++i){
up = up * (a-i) % p;
down = down * (i+1) % p;
}
return up * qpow(down, p-2, p) % p;
}
ll lucas(ll a, ll b, ll p){
if (b == 0)
return 1;
return comb(a%p, b%p, p) * lucas(a/p, b/p, p) % p;
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while (~scanf("%d%d", &n, &m))
printf("%I64d\n", lucas(m+n-4, m-2, mod));
return 0;
}