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出发,到给定的点,可以走右下,左下,和下三条路。问到给定的点有多少条路;
题解:
给一个蜂窝形状的格子,初始在(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;
}