A - Counting Paths Gym - 101498D ——组合数

A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

Consider an infinite binary tree with each node has exactly two children, and two given integers a and b, count the number of paths in the infinite binary tree that satisfy the following rules:

The path starts from the root of the tree.
The length of the path is equal to a (The length of a path is the total number of edges from the root to the final node on that path).
The number of change of direction along the path is equal to b. Change of direction means, going to your right child if you are the left child of your parent or vise versa.
As the number of paths can be too large, print it (modulo 109 + 7).

Input
The first line of the input contains an integer T (1 ≤ T ≤ 105), where T is the number of the test cases.

Each test case has one line that contains two integers a and b (0 ≤ b < a ≤ 105), the length of the path and the number of change of direction along it.

Output
For each test case, print a single integer that represents the number of paths that satisfy the mentioned rules (modulo 109 + 7).

Example
Input
2
2 1
4 3
Output
2
2
去掉根节点,一开始有两条路走。其他的就是走a-1个节点有b种拐弯的可能

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll jc[100005];
void init()
{
    jc[0]=1;
    ll ret=1;
    for(ll i=1;i<=100000;i++)
    {
        ret=(ret*i)%mod;
        jc[i]=ret;
    }
}
ll qpow(ll a,ll b)
{
    ll ret=a;
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*ret)%mod;
        ret=(ret*ret)%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    init();
    while(t--)
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        a-=1;
        printf("%lld\n",2*jc[a]*qpow(jc[b]*jc[a-b]%mod,mod-2)%mod);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值