Hdu3037 Saving Beans Lucas定理

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

Description
Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees. 

Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
 
Input
The first line contains one integer T, means the number of cases. 

Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
 
Output
You should output the answer modulo p.
 
Sample Input
2
1 2 5
2 1 5 
 
Sample Output
3


题意:将m个物体放到n个不同的盒子中,允许不装满,问有多少种可能?

可以假设有n+1个盒子,第n+1个盒子装剩余的物体,那么由隔板法可得结果为C 【n+m】【m】,

再用下Lucas定理即可。

#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>

using namespace std;
#define LL long long
#define maxn 100005
LL f[maxn];
void init(LL p)
{
    LL i;
    f[0]=1;
    for(i=1;i<=p;i++){
        f[i]=f[i-1]*i%p;
    }
}
LL powmod(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 Lucas(LL n,LL m,LL p)
{
    LL ans=1;
    while(n&&m){
        LL nn=n%p,mm=m%p;
        if(nn<mm) return 0;
        ans=ans*f[nn]*powmod(f[mm]*f[nn-mm]%p,p-2,p)%p;
        n/=p;
        m/=p;
    }
    return ans;
}

int main() {
    LL n, m, p;
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld%lld",&n,&m,&p);
        init(p);
        printf("%lld\n",Lucas(n+m,m,p));
    }
    return 0;
}


Linux下安装FastDFS及生成缩略图 1. 安装编译环境 yum install git gcc gcc-c++ make automake vim wget libevent -y 2. 安装libfastcommon基础库 mkdir /root/fastdfs cd /root/fastdfs git clone https://github.com/happyfish100/libfastcommon.git --depth 1 cd libfastcommon/ ./make.sh && ./make.sh ins 阅读详情

相关推荐

SmsForwarder v6.4.0 + Flask API 实战:5步搭建手机验证码自动化接收服务

本文详细介绍了如何利用SmsForwarder v6.4.0和Flask API构建企业级验证码自动化处理系统。通过5个关键步骤,包括系统架构设计、Android端配置、Flask服务端实现、企业级部署方案以及实战应用场景扩展,帮助开发者高效处理短信验证码,提升自动化测试和数据采集的工作效率。

weixin_30292745的博客 377

HDU - 3037-Saving Beans[Lucas定理]

Saving Beans Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the sq...

lhc----- 275

大地坐标系与经纬度转换(一):大地坐标系简介

一、大地坐标系 大地坐标系是大地测量中以参考椭球面为基准面建立起来的坐标系。地面点的位置用大地经度、大地纬度和大地高度表示。 二、地心坐标系 ...

qq_42895926的博客 6万+

HDU 3037 Saving BeansLucas定理模板题)

Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel fami...

weixin_30323961的博客 196

HDU 3037 Saving BeansLucas的套用)

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2234    Accepted Submission(s): 812 Problem Description Although winter is

风尘_浪子 767

hdu 3037 Saving Beans

Saving Beans Time Limit: 6000/3000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2079Accepted Submission(s): 748 Problem Description Although winter is far a...

weixin_34315665的博客 218

HDU - 3037 Saving Beans (JAVA)

Saving Beans Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks t...

Tan_JX 265

HDU 3037 Saving Beans lucas定理小试

分析:不超过m个在n个篮子里,

行走世间,皆为妖怪 630

[HDU3037] Saving Beans Lucas定理

传送门Problem DescriptionAlthough winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family...

LvYanchang的博客 332

hdu3037Saving Beans lucas定理

hdu3037 lucas 对组合数取模 lucas(n,m)=C(n%mod,m%mod)*lucas(n/mod,m/mod)%mod; lucas(n,0)=1; #include #include using namespace std; __int64 jc[100010]; __int64 exgcd(__int64 a,__int64 b,__int64

Time flies 1217

hdu 3037 Saving Beans Lucas定理

题意:将不超过m颗豆子存储在n颗不同的树上,求种类数结果模p,输入的p一定是质数。 用隔板法求种类数:http://blog.sina.com.cn/s/blog_7cc6f2770100red3.html 题解:http://m.blog.csdn.net/discreeter/article/details/70244319 费马小定理求逆元:http://blog.csdn.net/c

chen_minghui的博客 289

HDU 3037 Saving BeansLucas定理

组合数取模 Lucas定理入门

Ab.Ever 354

HDU3037 Saving Beans Lucas 定理+逆元

HDU3037 Saving Beans 题目相当于求n个数的和不超过m的方案数。 如果和恰好等于m,那么就等价于方程x1+x2+...+xn = m的解的个数,利用插板法可以得到方案数为: (m+1)x(m+2)...(m+n-1)  = C(m+n-1,n-1) = C(m+n-1,m) 现在就需要求不大于m的,相当于对i = 0,1...,m对C(n+i-1,i)求和,根据公式C(n...

Qianyri的博客 295

HDU 3037Saving Beans Lucas定理模板

http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板。 现在才写,noip滚粗前兆QAQ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int ...

as2886089的博客 141

hdu 3037 Saving Beanslucas定理模板)

题意: 求在n棵树上摘不超过m颗豆子的方案,结果对p取模。 解析: 题目可以转换成 x1+x2+……+xn=mx_1+x_2+……+x_n=m 有多少组解,m在题中可以取0~m。 利用插板法可以得出 x1+x2+……+xn=mx_1+x_2+……+x_n=m 解的个数为Cn−1n+m−1=Cmn+m−1C_{n+m-1}^{n-1}=C_{n+m-1}^m; 则题

小G的ACM之路 1526

HDU - 3037 Saving Beans lucas定理

题目:给出n,m,p,求C(n+m,m)然后对p取模 p 代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include #include #include #include #include #include #include #include #include #include #include #include

ACVevtor的博客 347

hdu3037】【Lucas定理Saving Beans

经过推导答案为C(n + m,m) 但是n和m的范围相当大,所以我们可以使用Lucas定理,它可以用来解决大组合数取模的问题。 定理的内容: A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。 则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod

I'm dreamer 725

hdu 3037 saving beans (lucas定理)

题目大意:求在n棵树保存不超过m个豆子的方法数,结果对p取模。 (n、m 分析: 由插板法,在n棵树保存i个豆子方案数为C(n+i-1,i)。 不超过m个豆子,则方案数为C(n+0-1,0)+C(n+1-1,0)+……+C(n+m-1,m)。 根据组合数性质:C(n,m)=C(n-1,m-1)+C(n-1,m),可将上式化简为C(n+m,m) 即所求结果为C(n+m,m) 考虑

白学病患者的专栏 485

[Lucas定理] hdu 3037 Saving Beans

Lucas 定理:A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。 则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  modp同 即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)  For non-ne

Enmity_Dark 617

HDU 3037 Saving Beans Lucas定理

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3037题意:把不超过m个豆子放在n棵树上,对于任一棵树,可以放任意个豆子(包括0),问方案有多少种。思路:如果恰好有m个豆子时,那么就是求把m个豆子分成n组的方案数,可以用隔板法,由于允许某些组为空,所以人为地为每组都先添加一个豆子,分完后再去掉即可,于是有n+m个豆子,有n+m-1个空可以放隔板,分成n组需

弱智就要多努力 536

HDU 3037 Saving BeansLucas定理

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p, 用Lucas定理求大组合数取模的值 代码: #include #include #include using namespace std; int t; long long n, m, p; long long pow(long lon

Remilia's 900

hdu】【P3037】【Saving Beans】【题解】【Lucas定理

传送门:acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理入门题 Lucas定理: c(n,m)%p=

退役狗的专栏 953
上一篇: 线性筛求莫比乌斯反演函数+分块+前缀和
下一篇: Lucas定理模板
Megumin
博客等级 码龄10年 3粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值