题目链接: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)
规律为:
详见代码。
#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,y
ll 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;
}