hdu_5698_瞬間移動

來源:互聯網
上載者:User

標籤:解決   alt   tps   name   測試資料   const   def   log   紅色   

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

Input多組測試資料。

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

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 longconst ll mod=1000000007;#define maxn 70ll 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;    }}

  

hdu_5698_瞬間移動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.