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.
Sample Input
1 1 3 2 100000 100000
Sample Output
1 3 1
Hint
Source
思路:若只能向↙或↘走,从(1,1)->(x,y),总共要走tot=(x-1)步,其中向↘要走r=(y-1)步,向↙要走l=(x-y)步,那么总的走法就是C(tot,r)(或C(tot,l))。 若还能向↓走,因为一步↓相当于一步↙加上一步↘,所以若向↓走i步, 总共就只要走tot'=(tot-i)步,向↘走r'=(r-i)步,向↙走l'=(l-i)步,(其中0<=i<=min(l,r)), 那么总的走法数就是C(tot',i)+C(tot'-i,r)(表达形式不止一种)。
code:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
typedef long long ll;
ll fac[110000];
void init(){
fac[0] = fac[1] = 1;
for(int i = 2; i <= 100000; i++){
fac[i] = (fac[i-1] * i) % mod;
}
}
ll q_pow(ll a,ll b){
ll ans = 1;
while(b){
if(b & 1)
ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans % mod;
}
ll C(ll n,ll m){
if(m > n) return 0;
return fac[n] * q_pow(fac[m] * fac[n-m], mod - 2) % mod;
}
ll Lucas(ll n,ll m){
if(m == 0) return 1;
else return (C(n%mod,m%mod) * Lucas(n/mod,m/mod)) % mod;
}
int main(){
init();
ll x,y;
while(~scanf("%lld%lld",&x,&y)){
ll all = x - 1;
ll r1 = y - 1;
ll l1 = x - y;
ll down = min(l1,r1);
ll ans = 0;
for(ll i = 0; i <= down; i++){
ll tot = all - i;
ll l = l1 - i;
ll r = r1 - i;
ans = (ans + Lucas(tot,i) % mod * Lucas(tot-i,l) % mod) % mod;
}
printf("%lld\n",ans);
}
return 0;
}
六边形网格路径算法
本文探讨了在一个六边形网格上从起始点到目标点的所有可能路径数量的计算方法。采用组合数学原理,通过Lucas定理解决路径计算问题,并提供了具体的C++实现代码。

被折叠的 条评论
为什么被折叠?



