规律是杨辉三角,也就是求排列组合。因为要取模,所以需要用到逆元。
#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long ll;
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const int mod = 1000000007;
int n,m;
//求ax = 1( mod m) 的x值,就是逆元(0<a<m)
ll inv(long long a,long long m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
//a<=b
ll C(int a,int b)
{
ll t1=1,t2=1;
for(int i = b ; i>=(b-a+1) ;i--)t1 = t1*i%mod;
for(int i = a ; i>=1 ;i--)t2 = t2*i%mod;
return t1*inv(t2,mod)%mod;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n>m)swap(n,m);
printf("%I64d\n", C(m-2,n+m-4) );
}
return 0;
}
PS:为何需要逆元
以下内容来自:http://www.cnblogs.com/linyujun/p/5194184.html
数论倒数,又称逆元(因为我说习惯逆元了,下面我都说逆元)
数论中的倒数是有特别的意义滴
你以为a的倒数在数论中还是1/a吗
(・∀・)哼哼~天真
先来引入求余概念
(a + b) % p = (a%p + b%p) %p (对)
(a - b) % p = (a%p - b%p) %p (对)
(a * b) % p = (a%p * b%p) %p (对)
(a / b) % p = (a%p / b%p) %p (错)
为什么除法错的
证明是对的难,证明错的只要举一个反例
(100/50)%20 = 2 ≠ (100%20) / (50%20) %20 = 0
对于一些题目,我们必须在中间过程中进行求余,否则数字太大,电脑存不下,那如果这个算式中出现除法,我们是不是对这个算式就无法计算了呢?
答案当然是 NO (>o<)
这时就需要逆元了