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 ...
随机推荐
- asp.net调用微信扫一扫功能
1.页面引用http://res.wx.qq.com/open/js/jweixin-1.0.0.js 2.前台代码 function shaomiao() { wx.config({ debug: ...
- Swagger使用教程 SwashbuckleEx
一.前言 自从之前写了一篇<Webapi文档描述-swagger优化>这篇文章后,欠了大家一篇使用文档的说明,现在给大家补上哈. 二.环境 .Net Framework 4.5 WebAp ...
- centos6.5安装mysql
1.yum -install mysql mysql-server -y 2.修改mysql的root的密码 登录:mysql -uroot 修改密码: use ...
- 《SQLSERVER2012实施与管理实战指南》前4章节笔记内容
<SQLSERVER2012实施与管理实战指南>前4章节笔记内容 <SQLSERVER2012实施与管理实战指南>的前面4章是<SQLSERVER企业级平台管理实践> ...
- c#为字段设置默认值,以及构造函数初始化List对象。
1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...
- 适用于 Windows VM 的 Azure 示例基础结构演练
本文是更广泛系列的一部分,当你在 Azure 中构建应用程序基础结构时,这些系列向你提供设计注意事项和准则. 用户可以查看系列中的其他主题. 尽管用户可以在 Azure 中快速地构建出开发/测试环境, ...
- SQL Server 中为何拥有db_owner权限的账号删除不掉数据库
今天在公司的SQL Server服务器上,使用了一个只有public和dbcreator角色的账号"user1"在SMSS中去删除一个数据库,但是死活报错说没有权限,报错如下: D ...
- sql server数据导入导出方法统计
常用的数据量不是很大的情况的几种方法:转载地址 http://www.cnblogs.com/changbluesky/archive/2010/06/23/1761779.html 大数据量的推荐导 ...
- 2.2Python基础语法(二)之运算符
返回总目录 目录: 1.Python运算符的分类 2.算数运算符 3.复合运算符 4.比较运算符 5.逻辑运算符 (一)Python运算符的分类: (二)算数运算符: 注意下面三种算数符号: 1.** ...
- 【解决方案】chrome打开新标签页自动打开chrome://newtab并且跳转到谷歌香港
简述天,昨天开始遇到这个问题,还没有留心,结果今天多次使用chrome的时候,就发现有些不对了..打开chrome的新标签页,结果出现了自动跳转的问题我自动跳转的是下面这个网页:https://www ...