Hdu 3037 Saving Beans(Lucus定理+乘法逆元)
Saving Beans
Time Limit: 3000 MS Memory Limit: 32768 K
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 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
3
Hint
For sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on.
The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are:
put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
题意:
由n个不同的盒子,在每个盒子中放一些球(可以不放),使得总球数<=m,求方案数模p后的值.
1<=n,m<=10^9,1< p < 10^5,保证p是素数.
题解(第一次用数学编辑器2333)
#include<iostream>
#include<cstdio>
#define MAXN 100001
#define LL long long
using namespace std;
LL M[MAXN];
LL mi(LL a,LL b,LL p)
{
LL tot=1;
while(b)
{
if(b&1) tot=tot*a%p;
a=a*a%p;
b>>=1;
}
return tot;
}
LL C(LL n,LL m,LL p)
{
if(m>n) return 0;
LL tot=1;
return M[n]*mi(M[n-m],p-2,p)%p*mi(M[m],p-2,p)%p;
}
LL lucus(LL n,LL m,LL p)
{
if(!m) return 1;
return lucus(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
int main()
{
LL n,m,p,t;
cin>>t;
while(t--)
{
cin>>n>>m>>p;
M[0]=1;
for(int i=1;i<=p;i++) M[i]=M[i-1]*i%p;
printf("%lld\n",lucus(n+m,m,p));
}
return 0;
}
Hdu 3037 Saving Beans(Lucus定理+乘法逆元)的更多相关文章
- hdu 3037 Saving Beans Lucas定理
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3037 Saving Beans(Lucas定理的直接应用)
解题思路: 直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题. #include <string.h> #include ...
- hdu 3037 Saving Beans(组合数学)
hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以 ...
- hdu 3037——Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3037 Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- HDU 3037 Saving Beans (数论,Lucas定理)
题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...
- HDU 3037 Saving Beans (Lucas法则)
主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理 ...
- bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)
Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...
随机推荐
- Docker 安装入门 Centos Linux安装Docker 部署mysql
这次购买了阿里云云服务器,并且安装了Centos 7.5 学习使用Docker, 确认版本信息 Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上. Dock ...
- C# 练习题 将一个正整数分解质因数
题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:(1)如果这个质数恰等于n,则说明分解质因数的过程 ...
- igel udc2 config
igel udc2 config 系统安装盘下载地址 http://www.myigel.biz/?forcedownload /config/bin/igelone_config #!/bin/sh ...
- poi读取excel的列和删除列
(各自根据具体的poi版本进行相应的替换即可) package com.br.loan.strategy.common.utils; import lombok.extern.slf4j.Slf4j; ...
- WPF弹出进度条
环境:vs2015 ,.net 4.6 源码地址:https://pan.baidu.com/s/1FoujUMst1luomPf0ZfCLUQ 提取码:trzj 说明: 1.进度条是在winFrom ...
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
- selenium模拟鼠标键盘操作
简单操作: 1.点击(鼠标左键)页面按钮:click() 2.清空输入框:clear() 3.输入字符串:send_keys()submit提交表单: 1.一般情况可以点击搜索按钮来搜索 2.也可以用 ...
- Centos7搭建DockerRegistry
1. 说明 以下使用系统centos7,64位,镜像为CentOS-7-x86_64-Minimal-1804,均已root用户进行操作 2. 安装Registry Docker Registry 是 ...
- 函数式接口(Functional Interface)
原文链接:https://www.cnblogs.com/runningTurtle/p/7092632.html 阅读目录 什么是函数式接口(Functional Interface) 函数式接口用 ...
- UVA572 Oil Deposits DFS求解
小白书上经典DFS题目. 1. 递归实现 // from: https://www.cnblogs.com/huaszjh/p/4686092.html #include <stdio.h> ...