题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据。

分析:预处理出阶乘和其逆元。但如果每次O(m)累加,那么会超时。

定义 S(n, m) = sigma(C(n,m))。有公式:S(n,m) = S(n,m-1) +C(n,m)以及S(n,m) = 2*S(n-1,m) - C(n-1,m)。

这样就可以在O(1)的时间中计算出S(n+1,m),S(n-1,m),S(n,m+1),S(n,m+1)。可以用莫队离线处理T组查询。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int maxn =1e5+;
  5. const int mod = 1e9+;
  6. LL fac[maxn],inv[maxn];
  7. LL rev2;
  8. LL qpow(LL b,int n){
  9. LL res=;
  10. while(n){
  11. if(n&) res=res*b%mod;
  12. b = b*b%mod;
  13. n>>=;
  14. }
  15. return res;
  16. }
  17.  
  18. LL Comb(int n,int k)
  19. {
  20. return fac[n]*inv[k]%mod *inv[n-k]%mod;
  21. }
  22.  
  23. void pre()
  24. {
  25. rev2 = qpow(,mod-);
  26. fac[]=fac[]=;
  27. for(int i=;i<maxn;++i) fac[i]=i*fac[i-]%mod;
  28. inv[maxn-]=qpow(fac[maxn-],mod-);
  29. for(int i=maxn-;i>=;i--) inv[i]=inv[i+]*(i+)%mod;
  30. }
  31.  
  32. int pos[maxn];
  33.  
  34. struct Query{
  35. int L,R,id;
  36. bool operator <(const Query& p) const{
  37. if(pos[L]==pos[p.L]) return R<p.R;
  38. return L<p.L;
  39. }
  40. }Q[maxn];
  41. LL res;
  42. LL ans[maxn];
  43.  
  44. inline void addN(int posL,int posR)
  45. {
  46. res = (*res%mod - Comb(posL-,posR)+mod)%mod;
  47. }
  48.  
  49. inline void addM(int posL,int posR)
  50. {
  51. res = (res+Comb(posL,posR))%mod;
  52. }
  53.  
  54. inline void delN(int posL,int posR)
  55. {
  56. res = (res+Comb(posL-,posR))%mod *rev2 %mod;
  57. }
  58.  
  59. inline void delM(int posL,int posR)
  60. {
  61. res = (res-Comb(posL,posR)+mod)%mod;
  62. }
  63.  
  64. int main()
  65. {
  66. #ifndef ONLINE_JUDGE
  67. freopen("in.txt","r",stdin);
  68. freopen("out.txt","w",stdout);
  69. #endif
  70. int T,N,M,u,v,tmp,K;
  71. int a,b;
  72. pre();
  73. int block = (int)sqrt(1.0*maxn);
  74. scanf("%d",&T);
  75. for(int i=;i<=T;++i){
  76. scanf("%d%d",&Q[i].L,&Q[i].R);
  77. pos[i] = i/block;
  78. Q[i].id = i;
  79. }
  80. sort(Q+,Q+T+);
  81. res=;
  82. int curL=,curR=;
  83. for(int i=;i<=T;++i){
  84. while(curL<Q[i].L) addN(++curL,curR); //ok
  85. while(curR<Q[i].R) addM(curL,++curR); //ok
  86. while(curL>Q[i].L) delN(curL--,curR); //ok
  87. while(curR>Q[i].R) delM(curL,curR--); //ok
  88. ans[Q[i].id] = res;
  89. }
  90. for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
  91. return ;
  92. }

HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)的更多相关文章

  1. HDU - 6333 Problem B. Harvest of Apples (莫队)

    There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...

  2. Problem B. Harvest of Apples 莫队求组合数前缀和

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

  3. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

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

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

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

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

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

随机推荐

  1. C++ 类模板三(类模版中的static关键字)

    //类模版中的static关键字 #include<iostream> using namespace std; /* 类模板本质上是c++编译器根据类型参数创建了不同的类, c++编译器 ...

  2. C语言 函数指针一(函数指针的定义)

    //函数指针 #include<stdio.h> #include<stdlib.h> #include<string.h> //函数指针类型跟数组类型非常相似 / ...

  3. Java多线程基础知识总结

    2016-07-18 15:40:51 Java 多线程基础 1. 线程和进程 1.1 进程的概念 进程是表示资源分配的基本单位,又是调度运行的基本单位.例如,用户运行自己的程序,系统就创建一个进程, ...

  4. cobbler default system 网络安装时主机的menu上只有一个local选项

    问题:使用cobbler default system 做pxe网络安装时,主机启动后安装menu上只有一个local选项,看不到对应的system名字 解决:cobbler default syst ...

  5. iOS 保存异常日志

    // // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...

  6. iOS Xcode之SVN(remove git)

    项目用SVN比较多,所以大家都把精力放在如何在XCODE上使用SVN.     配置SVN当然是很简单,但提交都默认出现git的提交窗,否则要到repositories界面去提交.   目前没有找到什 ...

  7. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. delphi --批量添加

    公共批量添加方法 function BatchSQL(DC : TADOConnection; Qry : TADOQuery; StrSQL : TStrings): Boolean; var i ...

  9. 巨蟒python全栈开发flask4

    1.偏函数 2.ThreadingLocal线程安全 空间换取时间 3.LocalStack 4.RunFlask+request 5.请求上文 6.请求下文

  10. JavaScript数据类型转换汇总

    ECMAScirpt中的数据类型:undefined.Null.Boolean.Number.String.Object 对一个值使用typeof操作符可能返回下列某个字符串: number(数字). ...