Counting Paths Gym - 101498D (modulo combined numbers)

Topic link: https://vjudge.net/problem/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

Question meaning: Enter a and b for each group of cases. The meaning of the question is to find 2*C (a-1, b) mod (1e9+7), directly use the combined number to find the modulus template.

Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;

typedef long long LL;
const LL maxn(1000005), mod(1e9 + 7);
LL Jc[maxn];

void calJc()    //求maxn以内的数的阶乘
{
    Jc[0] = Jc[1] = 1;
    for(LL i = 2; i < maxn; i++)
        Jc[i] = Jc[i - 1] * i % mod;
}

//拓展欧几里得算法求逆元
void exgcd(LL a, LL b, LL &x, LL &y)    //拓展欧几里得算法
{
    if(!b) x = 1, y = 0;
    else
    {
        exgcd(b, a % b, y, x);
        y -= x * (a / b);
    }
}

LL niYuan(LL a, LL b)   //求a对b取模的逆元
{
    LL x, y;
    exgcd(a, b, x, y);
    return (x + b) % b;
}


//费马小定理求逆元
//LL pow(LL a, LL n, LL p)    //快速幂 a^n % p
//{
//    LL ans = 1;
//    while(n)
//    {
//        if(n & 1) ans = ans * a % p;
//        a = a * a % p;
//        n >>= 1;
//    }
//    return ans;
//}
//
//LL niYuan(LL a, LL b)   //费马小定理求逆元
//{
//    return pow(a, b - 2, b);
//}

LL C(LL a, LL b)    //计算C(a, b)
{
    return Jc[a] * niYuan(Jc[b], mod) % mod
        * niYuan(Jc[a - b], mod) % mod;
}




int main()
{
    int T;
    LL a,b,ans;
    calJc();
    scanf("%d",&T);
    while(T--)
    {
       scanf("%d%d",&a,&b);
       ans=C(a-1,b);
       ans*=2;
       ans%=mod;
       printf("%lld\n",ans);
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325950253&siteId=291194637