• HLG1744组合数学问题与lucas定理运用


    The figure below shows Pascal's Triangle:

     

    Baby H divides Pascal's Triangle into some Diagonals, like the following figure:

     

    Baby H wants to know the sum of K number in front on the Mth diagonal. Try to calculate it.

    Input

    There are multiple test cases. The first line is a positive integer T (1<=T<=100) indicating the number of test cases.

    For each test case:

    Line 1. Two positive integers M and K (1<= M , K <= 100 000).

    Output

    For each test case, output the sum of K number in front on the Mth diagonal in one line. The answer should modulo to 20 000 003.

    Sample Input
    2
    2 3
    3 4
    Sample Output
    6
    20

     思路公式,否则超时

    C(N,M)+C(N+1,M)+C(N+2,M)==C(N+3,M+1)

    代码#include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    LL exp_mod(LL a, LL b, LL p)
    { LL res = 1;
        while(b != 0)
        {
            if(b&1) res = (res * a) % p;
            a = (a*a) % p;
            b >>= 1;
        }
        return res;
    }
    LL Comb(LL a, LL b, LL p)
    {
        if(a < b)   return 0;
        if(a == b)  return 1;
        if(b > a - b)   b = a - b; LL ans = 1, ca = 1, cb = 1;
        for(LL i = 0; i < b; ++i)
        {
            ca = (ca * (a - i))%p;
            cb = (cb * (b - i))%p;
        }
        ans = (ca*exp_mod(cb, p - 2, p)) % p;
        return ans;
    }

    LL Lucas(int n, int m, int p)
    {
        LL ans = 1;

        while(n&&m&&ans)
        {
            ans = (ans*Comb(n%p, m%p, p)) % p;
            n /= p;
            m /= p;
        }
        return ans;
    }

    int main()
    {

        int n, m, t;
        scanf("%d",&t);
        int  p=20000003;
        while(t--)
        {
              scanf("%d%d",&m,&n);
                printf("%lld ", Lucas(m+n-1, m, p));

        }
        return 0;
    }

  • 相关阅读:
    Flex布局-语法篇
    css grid布局
    js 方法的一些简写和技巧
    css瀑布流
    js防抖和节流
    js循环
    两行css代码实现居中元素
    手写代码部分
    BigDecimal类的概念和使用
    Math类的概念和使用
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4403536.html
Copyright © 2020-2023  润新知