HEX 山东省第八届省赛D题

本文介绍了一个基于六边形网格平面的路径计算问题,通过定义移动步骤为从当前网格向下、下左或下右移动到相邻网格,计算从起始点到达目标点的所有可能路径数量,并给出了解决该问题的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HEX

Time Limit: 4000MS Memory Limit: 131072KB
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
Hint
Author
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)


向左是(1,0)向右是(1,1)向下是(2,1)
有(1,1)+x(1,0)+y(2,1)+z(1,1)==(a,b);
z+y=b-1;
x+y=a-b;
然后循环一下y;表示出x,z;组合数
注意一下组合数的求法,小心TLE。

#include <cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
#define maxn 100007
typedef long long ll;
const int mod = 1e9+7;

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 );
}
int main()
{
    int a,b;
    ll ans;
    init();
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        ans=0;
        int t=a-b;///x+y
        int k=b-1;///y+z
        int u=min(t,k);
        for(int i=0; i<=u; i++)
        {
            int o=t-i;///x
            int p=k-i;///z
            ans+=C(i+o+p,o)*C(i+p,i)%mod;///C(x+y+z,x)*C(y+z,y)*C(z,z)
            ans%=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值