標籤:
題目連結: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)——數學題)