问题 1300. – HEX
1300: HEX
时间限制: 4 秒 内存限制: 128 MB提交: 26 解决: 9
提交 状态
题目描述
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.
输入
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.
输出
each case output one integer in a line, the number of ways to get to the
destination MOD 1000000007.
样例输入
1 1
3 2
100000 100000
样例输出
1
3
1
提示
来源
题意 就是从 1,1开始每次向 ↙ ↓ ↘这几个方向移动,给一个坐标,问到那个点有多少种走法
这肯定是排列组合题目,先把到一个点向左向右走的步数算出来,向左就是 n-m 向右是 m-1 (大佬推出来的规律) ,设 C是竖着走的步数,则走到一个点需要 l+r+c步 ,先把向左向右走看成一个 C(l+r+c,c)(向左向右走的所有情况的个数)这就是 C(l+r+c,c) C(l+r,r)就是所有情况,然后枚举c就可以了
看的大佬的博客都是(l+r+c) ! / ( l! * r! c!) 就是 C(l+r+c,c) C(l+r,r)化简的结果,会减一半时间
注意 除法取余 逆元(需打表,要不会超时)
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
ll re[100009];
ll vi[100009];
ll poww(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=(ans*a)%mod;
b>>=1;
a=(a*a)%mod;
}
return ans;
}
void intt()
{
re[0]=1;
vi[0]=1;
for(int i=1;i<=100003;i++)
{
re[i]=(i*re[i-1])%mod;
vi[i]=poww(re[i],mod-2)%mod;
//cout<<vi[i]<<endl;
}
}
ll C(int a,int b)
{
return (re[a]*(vi[a-b]%mod*vi[b]%mod)%mod)%mod;
}
int main()
{
// ios::sync_with_stdio(false);
ll n,m;
intt();
while(cin>>n>>m)
{ ll ans=0;
//int i=m;
int l=n-m,r=m-1;
int c=0;
// int k=l+r-1;
while(l>=0&&0<=r)
{
ans+=C(l+r+c,c)*C(l+r,r)%mod;
// ans+=re[l+r+c]*(vi[l]%mod*vi[r]%mod*vi[c]%mod)%mod;
ans%=mod;
c++;
l--;
r--;
}
cout<<ans<<endl;
}
return 0;
}