HEX-山东省第八届省赛D题-组合数学

HEX

Time Limit: 4000 ms  Memory Limit: 131072 KiB
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.

Sample Input
1 1
3 2
100000 100000
Sample Output
1
3
1
Hint
Source
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)
题意:

给你一个菱形的棋盘,然后从1 1出发,到给定的点,可以走右下,左下,和下三条路。问到给定的点有多少条路;

题解:

给一个蜂窝形状的格子,初始在(1,1)点,其可以向左走,向下走,向右走,
    向左走的坐标变化为(1,0),向下走的坐标变化为(2,1),向右走的坐标变化为(1,1)
    我们可以设 向左走的次数为 a, 向下走的次数为 b,向右走的次数为 c
    当从(1,1)走到(x,y)点的路径满足以下关系式
    (1,1) + a(1,0) + b(2,1) + c(1,1) = (x,y)
    1 + a + 2b + c = x;
    1 + b + c = y

    于是我们可以枚举出a, 通过关系式 算出b,c
    对于当前的 a,b,c;我们可以其所有组合为 C(a+b+c,a)*C(b+c,b)*C(c,c);

    之后求和及我们想要的结果 

代码:

/**
    给一个蜂窝形状的格子,初始在(1,1)点,其可以向左走,向下走,向右走,
    向左走的坐标变化为(1,0),向下走的坐标变化为(2,1),向右走的坐标变化为(1,1)
    我们可以设 向左走的次数为 a, 向下走的次数为 b,向右走的次数为 c
    当从(1,1)走到(x,y)点的路径满足以下关系式
    (1,1) + a(1,0) + b(2,1) + c(1,1) = (x,y)
    1 + a + 2b + c = x;
    1 + b + c = y

    于是我们可以枚举出a, 通过关系式 算出b,c
    对于当前的 a,b,c;我们可以其所有组合为 C(a+b+c,a)*C(b+c,b)*C(c,c);
    之后求和及我们想要的结果
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e5+10;
ll power(ll a,ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
ll fac[maxn],inv[maxn];
void init() {
    fac[0] = fac[1] = 1;
    inv[0] = inv[1] = 1;
    for(int i=2;i<maxn;i++) {
        fac[i] = fac[i-1] * i % mod;
        inv[i] = power(fac[i],mod-2);
    }
}
ll C(ll n,ll m) {
    return fac[n] * (inv[m] * inv[n-m] % mod) % mod;
}
int main()
{
    init();
    int x,y;
    while(~scanf("%d%d",&x,&y))
    {
        ll ans = 0;
        for(int c=0;c<y;c++) {
            int b = y - 1 - c;
            int a = x - 1 - 2*b - c;
            if(a >= 0 && b >= 0 && c>= 0)
                ans = (ans + C(a+b+c,a) * C(b+c,b) % mod) % mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值