hdu_5698_瞬間移動

有一個無限大的矩形,初始時你在左上角(即第一行第一列),每次你均可以選擇一個右下方格子,並瞬移過去(如從下圖中的紅色格子能直接瞬移到藍色格子),求到第 nn行第mm列的格子有幾種方案,答案對10000000071000000007取模。

Input多組測試數據。

兩個整數n,m(2n,m100000)n,m(2≤n,m≤100000)
Output一個整數表示答案Sample Inputios

4 5

Sample Output測試

10

盧卡斯定理解楊輝三角 楊輝三角第n行的m個數可表示爲C(n-1,m-1)
Lucas定理是用來求 C(n,m) mod p,p爲素數的值,用來解決大組合數求模是頗有用的。最大的數據處理能力是p在10^5左右。
C(n,m) = C(n-1,m-1) + C(n-1,m).



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const ll mod=1000000007;
#define maxn 70
ll pow(ll a,ll n,ll mod)
{
    ll base=a,ret=1;
    while(n)
    {
        if(n&1) ret=(ret*base)%mod;
        base=(base*base)%mod;
        n>>=1;
    }
    return ret%mod;
}
ll C(ll n,ll k,ll p)
{
    if(k==n)
        return 1;
    if(n-k<k)
            k=n-k;
        ll ans=1;
        for(ll i=1;i<=k;i++)
        {
            ans=ans*(n-i+1)%p*pow(i,mod-2,p)%p;
        }
        return ans;
}
ll lucas(ll n,ll m,ll p)
{
    if(m==0)
        return 1;
    return C(n%p,m%p,p)*lucas(n/p,m/p,p)%p;
}

int main()
{
    ll n,m;
    while(~scanf("%lld%lld",&n,&m))
    {
        cout<<lucas(m+n-4,m-2,mod)<<endl;
    }
}
相關文章
相關標籤/搜索