hdu5698 瞬间移动(组合数取模&&卢卡斯定理&&快速幂)

有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第nn行第mm列的格子有几种方案,答案对10000000071000000007取模。

https://i-blog.csdnimg.cn/blog_migrate/6be27c7c8f0609e142553195f49b804c.jpeg

把这个方格填数:

0 0 0 0 0 0

0 1 1 1 1 1

0 1 2 3 4 5

0 1 3 6 10 15

发现和杨辉三角形有很大关系

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

杨辉三角形公式:C(N-1,M-1)

例如本题中的3行4列是三角形中的4行二列即C(3,1)

故由推理可得C(N+M-4,N-2)就是本题中的题解。

组合数取模用到快速积、lucas定理、组合数取模等知识点

代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
 
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 100050
#define MOD 1000000007
#define p 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
#define FOR(i , n) for(int i = 1 ;  i<= n ; i ++)
typedef pair<int , int> pii;
const long long INF= 0x3fffffff;
 
LL n,m;
 
LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1)
        {
            ans = ans * a % p;
            b--;
        }
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
 
LL C(LL n, LL m)//求组合数加取模
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a * quick_mod(b, p-2) % p) % p;
    }
    return ans;
}
 
LL Lucas(LL n, LL m)
{
    if(m == 0) return 1;
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;//lucas定理
}
 
int main()
{
    while(scanf("%I64d%I64d", &n, &m) != EOF)
    {
        if(n <= 1 || m <= 1)
        {
            printf("0\n");
            continue;
        }
        //m -= 2;
        n = n + m - 4;
        m -= 2;
        printf("%I64d\n", Lucas(n,m));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值