HEX(组合数+线性代数)

Problem Description

On a plain of hexagonal grid, we define a step as one move from the current grid to the lower/lower-left/lower-right grid. For example, we can move from (1,1) to (2,1), (2,2) or (3,2).
In the following graph we give a demonstrate of how this coordinate system works.
Your task is to calculate how many possible ways can you get to grid(A,B) from gird(1,1), where A and B represent the grid is on the B-th position of the A-th line.
这里写图片描述
Input

For each test case, two integers A (1<=A<=100000) and B (1<=B<=A) are given in a line, process till the end of file, the number of test cases is around 1200.
Output

For each case output one integer in a line, the number of ways to get to the destination MOD 1000000007.
Example Input

1 1
3 2
100000 100000
Example Output

1
3
1

//jnf 搞数学真强。。。 
//从(1,1) 到(a,b) ,可供选择的方法只有 (1,0),(2,1),(1,1)
//故有 (1,1) + x*(1,0) + y*(2,1) + z*(1,1) = (a,b), 将其当做行列式来看
//所以可以求出   x+y=a-b; y+z=b-1;  所以  0 <= y <= min(a-b,b-1);
//通过枚举 y 的值求出 x,z;
// 在根据组合数学来求出 当前下的方法数 ,即 res = C(x+y+z,x)*C(y+z,y);
//将所有的加起来就得到了结果 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<algorithm>

#define ll long long
#define inf 0x3f3f3f3f
#define maxn 100007  

const int mod = 1e9+7;
using namespace std;
ll f[maxn];
ll v[maxn];

ll quick_pow(int a,int n)
{
    long long  tmp = a%mod;
    long long  ans = 1;
    while(n)
    {
        if(n%2) 
            {
                ans*= tmp;
                ans %= mod;
            }
        tmp *= tmp;
        tmp %= mod;
        n/=2;
    }
    return ans;
}
void init()
{
    f[0]=1;
    for(int i=1;i<maxn;i++)
        f[i] = (long long )(f[i-1] * i) % mod; 
    for(int i=maxn-1;i>=0;i--)
        v[i] = quick_pow(f[i],mod-2) % mod;
}

ll C(int n,int m)
{
    if(n==m || m == 0) return 1;
    return ( (long long) (f[n] * v[m]) % mod * v[n-m] %mod );
//  return ( (long long ) ( f[n] * quick_pow(f[m],mod-2) % mod) * quick_pow(f[n-m],mod-2)%mod ) % mod;
}

int main()
{
    int a,b;
    long long ans;
    init();
    while(~scanf("%d%d",&a,&b))
    {
        ans = 0;
        int t1=a-b;  // x+y
        int t2=b-1;  // y+z
        int tmp = min(t1,t2);
        for(int y=0;y <= tmp; y ++) // 枚举 y 
        {
            int x = t1 - y;
            int z = t2 - y;
            ans += C(x+y+z,x)*C(y+z,y) % mod;
            ans %= mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值