题目连接: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 (分块、数论)的更多相关文章

  1. 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) ...

  2. 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

  3. hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  4. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  5. 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 ...

  6. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

  7. HDU 6333 莫队+组合数

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  8. 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)    设 ...

  9. 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 ...

随机推荐

  1. webservice安全性浅谈

    原文地址:http://www.cnblogs.com/chhuic/archive/2009/11/19/1606109.html 做项目时,经常会用到WebService来通讯,但WebServi ...

  2. wc 统计文件的行数,字数,字符

    格式:wc 参数 文件  默认统计文件的行数,字数,字符. -l   统计有多少行数 -c   统计有多少个字节 -m  统计有多少个字符 -w 统计有多少个字数

  3. python基础学习6----字符串操作

    一.重复输出字符串 print('hello'*20)#输出20个hello 二.通过索引获取字符串中字符 print('helloworld'[2:])#输出lloworld 三.关键字 in pr ...

  4. 15 Top Paying IT Certifications In 2016: AWS Certified Solutions Architect Leads At $125K

    Each of the five Amazon Web Services (AWS) certifications brings in an average salary of more than $ ...

  5. 「2017 山东一轮集训 Day5」字符串

    题目 比较神仙的操作啊 首先先考虑一个串的做法,我们有两种:SA或SAM,其中SAM又有两种,拓扑图上的\(dp\)和\(parent\)上随便统计一下 显然这道题\(SA\)和\(parent\)树 ...

  6. php 微信公众号接入支付宝支付

    真是无力吐槽这个需求了,好端端的非要在微信公众号接入支付宝,都知道微信公众号是拒绝支付宝的,屏蔽了支付宝,所以在微信公众号接入支付宝的话就必须手动复制链接跳出微信内置浏览器,强制性打开web浏览器完成 ...

  7. Spark项目之电商用户行为分析大数据平台之(三)大数据集群的搭建

    Zookeeper集群搭建 http://www.cnblogs.com/qingyunzong/p/8619184.html Hadoop集群搭建 http://www.cnblogs.com/qi ...

  8. linux虚拟化课堂总结图2019_4_23

  9. 21天,搞定软件测试从业者必备的Linux命令

    开始之前,先同步一个结论: 对于软件测试从业者,如果你至今为止,还不懂Linux,或者完全没有接触Linux ,这是一件很危险和恐怖的事 . 此刻.现在.果断,学习Linux命令 . 如果你工作中,完 ...

  10. centos6.5 64位静默安装oracle 10G R2

    操作系统:CentOS release 6.5 (Final) 64位 oracle版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1 ...