题意 :  给了n课不同的树,要求将 0,1,2,3,4,5,...m个松果,分别放在n棵树上的方案数有多少,

我们这样考虑, 如果将m个相同的松果 放入n棵树中 , 转化一下,我们让每个点至少放1个松果,

将 摆成 一行 n+m 个 ,然后 n+m  中间会有n+m-1个空格 加末尾一个就说明有 n+m个 位置可以插入 东西

假设 第一个被插入的间隔是i表示 1-i之间全部是第一棵树的存放数量,,以此类推,当最后一个插入的间隔没有放在最末尾的时候,表明并没有 那么m个松果可以放的,这样我们求C(n+m,n)就可以了 C(n+m,p)用lucas 计算

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn =;
typedef long long LL;
LL fax[maxn];
void getfax(int p)
{
fax[]=;
for(LL i=; i<=p; i++)
{
fax[i]=(fax[i-]*i)%p;
}
}
void gcd(LL a, LL b, LL &d, LL &x, LL &y){
if(!b){
d=a; x=; y=;
}else{
gcd(b,a%b,d,y,x); y-=x*(a/b);
}
}
LL inv(LL a, LL n)
{
LL d,x,y;
gcd(a,n,d,x,y);
return (x+n)%n;
}
LL lucas(int n, int m, int p)
{
LL ans=;
while(n&&m)
{
int a=n%p,b=m%p;
if(a<b)return ;
ans=( ( (ans*fax[a])%p ) * ( inv(fax[b]*fax[a-b] , p)))%p;
n/=p;
m/=p;
}
return ans;
}
int main()
{
int n,m,p;
int cas;
scanf("%d",&cas);
for(int cc=; cc<=cas; cc++)
{
scanf("%d%d%d",&n,&m,&p);
getfax(p);
printf("%I64d\n",lucas(n+m,n,p));
} return ;
}

hdu3037 lucas的更多相关文章

  1. hdu3037 Lucas定理

    Lucas定理 Lucas(n,m,p)=c(n%p,m%p)* Lucas(n/p,m/p,p),其中lucas(n,m,p)=C(n,m)%p (这里的除号是整除) 证明——百度百科 题意:求n个 ...

  2. hdu3037 Saving Beans(Lucas定理)

    hdu3037 Saving Beans 题意:n个不同的盒子,每个盒子里放一些球(可不放),总球数<=m,求方案数. $1<=n,m<=1e9,1<p<1e5,p∈pr ...

  3. HDU3037 Saving Beans(Lucas定理+乘法逆元)

    题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...

  4. hdu3037(lucas定理)

    给定n,m,p   表示<=m个豆子放在n棵树上,一共有多少种方案数,  总的方案书mod p 如果将m个豆子放在n棵树上, 可以使用插板法 得到方案数是C(n+m-1,n-1) 那么将0< ...

  5. HDU3037 附Lucas简单整理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. [HDU3037]Saving Beans,插板法+lucas定理

    [基本解题思路] 将n个相同的元素排成一行,n个元素之间出现了(n-1)个空档,现在我们用(m-1)个“档板”插入(n-1)个空档中,就把n个元素隔成有序的m份,每个组依次按组序号分到对应位置的几个元 ...

  7. Lucas定理

    Lucas' theorem In number theory, Lucas's theorem expresses the remainder of division of the binomial ...

  8. HDU 3037(Lucas定理)

    对于很大的组合数不能用C(n, m) = C(n - 1, m) + C(n-1, m -1)来求,这里就用到Lucas定理. 模板题: hdu3037:模板如下: #include <cstd ...

  9. 大组合数Lucas

    https://blog.csdn.net/sr_19930829/article/details/39058487 LL Lucas(LL n, LL m, int p){ ; } Saving B ...

随机推荐

  1. LeetCode 559 Maximum Depth of N-ary Tree 解题报告

    题目要求 Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  2. java ftp上传文件

    /** * 上传文件到ftp * @param server * @param user * @param pwd * @param filenames */ public static void u ...

  3. springboot 整合swagger-ui

    一.添加maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springf ...

  4. BZOJ1280 Emmy卖猪pigs 网络流

    正解:网络流 解题报告: 传送门! 我网络流的基础题都还麻油做完就来做这个了,,,wsl,,, 首先想下最基础的构图方法 不难想到把猪圈和顾客分别当做节点,然后新建一个源点和汇点 然后考虑怎么连边,首 ...

  5. Webpack傻瓜式指南(转)

    add by zhj: 作者写了三篇文章,这是第一篇幅,另外两篇参见 https://zhuanlan.zhihu.com/p/20397902 https://zhuanlan.zhihu.com/ ...

  6. 关于Oxygen版 Eclipse JSP或html 中<option>标签嵌入jstl 如<c:if>出现报错

    Oxygen版 Eclipse JSP或html 中<option>标签嵌入jstl 如<c:if>出现报错 好好的学框架的整合,结果在<option>中加入< ...

  7. 弱网测试之基于fiddler+wanem完成

    如果不想搜集安装包,可以使用我分享的按照包,如下: WANem_3.0_Beta百度网盘下载链接:https://pan.baidu.com/s/1aMf5WBsfH7Mrl8iB2NjEYg 密码: ...

  8. laravel项目出现Non-static method Redis::hGet() cannot be called statically的解决方法

    早上ytkah在配置laravel项目中出现Non-static method Redis::hGet() cannot be called statically错误提示,很显然这是redis出问题了 ...

  9. Mac OSX上卸载Anaconda

    方案一 anaconda安装程序在~/.bash_profile脚本中新添加了一行,将anaconda bin目录添加到了$PATH环境变量中.所以你只需要删除anaconda目录,但是最好也从安装脚 ...

  10. ORACLE12C架构图