HDU 6333 Harvest of Apples (分块、数论)
题目连接:Harvest of Apples
题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n)。(样例组数为1e5)
题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将C(m,n)求出来了。但这样还是会超时,所以接下来要分块,每隔500个处理出C(1~m,n)的结果。然后还要知道一个性质 C(a,b) = ∑C(t1,x)*C(t2,y) (x+y<=b),这样我们就可以将给出的m和n分为两个组,其中一个组中元素的个数为500的倍数。然后我们对于另一个组枚举可能的t2然后求和,每次询问最大的操作次数就变成了500次。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
typedef pair<int,int> P;
const int MAX_N = 1e5+;
int N,M,T,S;
LL Jc[MAX_N];
LL ni_[MAX_N];
LL tran[][MAX_N];
LL tr[][];
//费马小定理求逆元
LL pow(LL a, LL n, LL p) //快速幂 a^n % p
{
LL ans = ;
while(n)
{
if(n & ) ans = ans * a % p;
a = a * a % p;
n >>= ;
}
return ans;
} LL niYuan(LL a, LL b) //费马小定理求逆元
{
return pow(a, b - , b);
} void calJc() //求maxn以内的数的阶乘
{
Jc[] = Jc[] = ;
for(LL i = ; i < MAX_N; i++)
Jc[i] = Jc[i - ] * i % mod;
}
void calni(){
for(int i=;i<MAX_N;i++){
ni_[i] = niYuan(Jc[i],mod);
}
} LL C(LL a, LL b) //计算C(a, b)
{
return Jc[a] * ni_[b] % mod
* ni_[a-b] % mod;
} void init(){
for(int i=;i<MAX_N/;i++){
for(int j=;j<MAX_N;j++){
tran[i][j] = C(*i,j);
if(j>) tran[i][j] = (tran[i][j] + tran[i][j-])%mod;
}
} for(int i=;i<;i++){
for(int j=;j<;j++){
tr[i][j] = C(i,j);
}
}
}
int main(){
calJc();
calni();
init();
//cout<<"OK"<<endl;
cin>>T;
while(T--){
LL a,b;
scanf("%lld%lld",&a,&b);
LL ans = ;
if(a < ){
for(int i=;i<=b;i++)
ans = (ans + tr[a][i])%mod;
}
else{
LL t1 = a / ;
LL t2 = a - t1*;
for(int i=;i<=min(b,t2);i++){
ans = (ans + tr[t2][i]*tran[t1][min(b - i,t1*)])%mod;
}
}
printf("%lld\n",ans); }
return ;
}
HDU 6333 Harvest of Apples (分块、数论)的更多相关文章
- HDU - 6333 Harvest of Apples
题意: T次询问,每次给出n,m.求sigma(k:0->m)C(n, k). 题解: 用离线莫队来做. 令S(n,m) = sigma(k:0->m)C(n, k). S(n+1, m) ...
- HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))
2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
- HDU 6333 莫队+组合数
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- hdu6333 Problem B. Harvest of Apples(组合数+莫队)
hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m) 设 ...
- Problem B. Harvest of Apples HDU - 6333(莫队)
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
随机推荐
- mongodb3.X权限配置
环境: CentOS6.8 mongodb3.4.1 1.连接mongodb数据库(如果mongo命令没有做环境变量配置,需要定位到有mongo命令的目录) [root@VM_118_34_cent ...
- TestLink笔记(一):环境配置+安装
注:转载请加上原文链接,谢谢! 本文的安装环境是Windows操作系统. (一) 前期准备 1.XAMPP下载(下载5.6的版本) https://www.apachefriends.org/ ...
- SQL Server 高并发Insert数据解析,实践
在现实的生产环境中,有可能遇到高并发insert的应用.在此应用时由于堆表(Heap)和聚集表的结构不同导致在高并发的情形下insert效率不尽相同.接下来我会简单的以测试用例来简要说明.并举例说明如 ...
- 在SQL Server中用好模糊查询指令LIKE (转载)
like在sql中的使用:在SQL Server中用好模糊查询指令LIKE:查询是SQL Server中重要的功能,而在查询中将Like用上,可以搜索到一些意想不到的结果和效果,like的神奇 一.一 ...
- PowerDesigner Code和Name设置大写tablespace设置,PK设置
1,PowerDesigner Code和Name设置大写 tool>MODEL OPTIONS 2.从oracle数据库导出的表结构默认包含了tablespace 删除tablespace方法 ...
- 《MySQL技术内幕:InnoDB存储引擎(第2版)》书摘
MySQL技术内幕:InnoDB存储引擎(第2版) 姜承尧 第1章 MySQL体系结构和存储引擎 >> 在上述例子中使用了mysqld_safe命令来启动数据库,当然启动MySQL实例的方 ...
- 176条DevOps人员常用的linux命令速查表
线上查询及帮助命令 文件和目录操作命令 查看文件及内容处理命令 文件压缩及解压缩命令 信息显示命令 搜索文件命令 用户管理命令 基础网络操作命令 深入网络操作命令 有关磁盘与文件系统的命令 系统权 ...
- http的断点续传
要实现断点续传的功能,通常都需要客户端记录下当前的下载进度,并在需要续传的时候通知服务端本次需要下载的内容片段. HTTP1.1协议(RFC2616)中定义了断点续传相关的HTTP头 Range和Co ...
- tali -f 和 tail -F 之间的区别
tail -f 等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止 tail -F 等同于--follow=name --retry ...
- python 进程池pool
进程池子 当你成千上万的业务需要创建成千上万的进程时,我们可以提前定义一个进程池 from multiprocessing import Pool p = Pool(10) #进程池创建方式,类似空任 ...