HEX 山东省第八届省赛D题

来源:互联网 发布:网络综合布线施工工序 编辑:程序博客网 时间:2024/05/16 15:34

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 13 2100000 100000

Example Output

131

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 100007typedef 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;}


原创粉丝点击