hdu 5698 瞬間移動(2016"百度之星" - 初賽(Astar Round2B)——數學題)

來源:互聯網
上載者:User

標籤:

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5698

瞬間移動  Accepts: 1018  Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others)Problem Description

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

Input

多組測試資料。

兩個整數n,m(2\leq n,m\leq 100000)n,m(2≤n,m≤100000)

Output

一個整數表示答案

Sample Input
4 5
Sample Output
10
解題思路:找規律,推出一個類似楊輝三角的圖形,精確的說是斜著楊輝三角的一半,根據楊輝三角的規律求結果。

規律為:

詳見代碼。

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int mod=1e9+7;typedef long long ll;//返回d=gcd(a,b);和對應於等式ax+by=d中的x,yll extend_gcd(ll a,ll b,ll &x,ll &y){    if(a==0&&b==0) return -1;//無最大公約數    if(b==0){x=1;y=0;return a;}    ll d=extend_gcd(b,a%b,y,x);    y-=a/b*x;    return d;}//*********求逆元素*******************//ax = 1(mod n)ll mod_reverse(ll a,ll n){    ll x,y;    ll d=extend_gcd(a,n,x,y);    if(d==1) return (x%n+n)%n;    else return -1;}ll c(ll m,ll n){    ll i,j,t1,t2,ans;    t1=t2=1;    for(i=n;i>=n-m+1;i--) t1=t1*i%mod;    for(i=1;i<=m;i++) t2=t2*i%mod;    return  t1*mod_reverse(t2,mod)%mod;}int main(){    ll n,m;    while(~scanf("%lld%lld",&n,&m))    {        if(n<m) swap(n,m);        printf("%lld\n",c(m-2,n+m-4));    }     return 0;}


hdu 5698 瞬間移動(2016"百度之星" - 初賽(Astar Round2B)——數學題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.