Problem B. Harvest of Apples(莫队离线)

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1792 Accepted Submission(s): 699

Problem Description

There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.

Input

The first line of the input contains an integer T (1≤T≤10^5) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤10^5).

Output

For each test case, print an integer representing the number of ways modulo 10^9+7.

Sample Input

2
5 2
1000 500

Sample Output

16
924129523

题意

  求∑mi=0∑i=0m\sum_{i=0}^{m}C(n,i)。

解题思路

  开始拿到这个题的时候还有些窃喜,诶,组合数求和,逆元搞一搞就行了,多捞哦!然后仔细一看数据范围,O(n^2)算法复杂度必超,尝试性的挑战了一下后台样例,果断TLE。然后我TM就找规律去了,woc杨辉三角,有点搞头,然后就没有然后了。。

  以前没有学过莫队算法,这真的是一种神奇的暴力解题方式。因为这题没有涉及到数据更新,所以我们可以把查询离线,然后分块。又因为(我们这里令∑mi=0∑i=0m\sum_{i=0}^{m}C(n,i)=S(n,m))

S(n+1,m)=2*S(n,m)-C(n,m)

S(n-1,m)=S(n,m)+C(n-1,m)

S(n,m+1)=S(n,m)+C(n,m+1)

S(n,m-1)=S(n,m)-C(n,m)

  PS:如果我们知道区间[L,R],就能在比较短的时间内求出[L−1,R],[L+1,R],[L,R−1],[L,R+1]的话,那就可以用莫队算法了。具体看代码吧。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+50;
const int mod = 1e9+7;long long inv[maxn],fac[maxn];
long long quickpow(long long a, long long b)
{long long ret = 1;a %= mod;while(b){if (b & 1) ret = (ret * a) % mod;b >>= 1;a = (a * a) % mod;}return ret;
}
void init()
{fac[0]=inv[0]=1;for(int i=1; i<maxn; i++){fac[i]=(fac[i-1]*i)%mod;  //得到阶乘inv[i]=quickpow(fac[i],mod-2);  //得到阶乘的逆元}
}int pos[maxn];//块
struct node
{int n,m,id;
} p[maxn];int cmp(node a,node b)//让相邻两个操作的操作次数尽可能的小
{if(pos[a.n]==pos[b.n]) return a.m<b.m;return pos[a.n]<pos[b.n];
}
int n,m;
long long ans,res[maxn];long long getnumber(int n,int m)
{if(n<m) return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
void addn(int n,int m)  /*S(n+1,m)=2*S(n,m)-C(n,m)*/
{ans=(2*ans%mod-getnumber(n,m)+mod)%mod;  //不加mod取模会错
}
void addm(int n,int m)  /*S(n-1,m)=S(n,m)+C(n-1,m)*/
{ans=(ans+getnumber(n,m+1))%mod;
}
void subn(int n,int m)  /*S(n,m+1)=S(n,m)+C(n,m+1)*/
{ans=(ans+getnumber(n-1,m))*inv[2]%mod;
}
void subm(int n,int m)  /*S(n,m-1)=S(n,m)-C(n,m)*/
{ans=(ans-getnumber(n,m)+mod)%mod;    //ts
}int main()
{
#ifdef DEBUGfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif // DEBUGinit();int t,block=(int)sqrt(1.0*maxn);scanf("%d",&t);for(int i=1; i<=t; i++) scanf("%d%d",&p[i].n,&p[i].m),p[i].id=i,pos[i]=i/block;sort(p+1,p+1+t,cmp);n=1,m=0,ans=1;for(int i=1; i<=t; i++){while(n<p[i].n) addn(n++,m);while(n>p[i].n) subn(n--,m);while(m<p[i].m) addm(n,m++);while(m>p[i].m) subm(n,m--);res[p[i].id]=ans;}for(int i=1; i<=t; i++) printf("%lld\n",res[i]);return 0;
}

HDU 6333 Problem B. Harvest of Apples(莫队离线)相关推荐

  1. HDU - 6333 Problem B. Harvest of Apples(莫队变形+思维+组合数学,好题)

    题目链接:点击查看 题目大意:给出n个苹果树,每个苹果树上可以摘一个苹果,问摘不超过m个苹果有多少种方式 题目分析:首先根据题意和样例可以推出,答案就是一个组合数之和,设C(n,m)为从n个数中选m个 ...

  2. [HDU](6333)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 ...

  3. 杭电多校 Harvest of Apples 莫队

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

  4. [数据结构专训][GXOI/GZOI2019]旧词,[hdu5118]GRE Words Once More!,[hdu6333]Problem B. Harvest of Apples

    文章目录 T1:[GXOI/GZOI2019]旧词 solution code T2:GRE Words Once More! solution code T3:Problem B. Harvest ...

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

  6. Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 题意:求c(n,0)到c(n,m)的和t组数据 每次累加一定会超时 得到公式s(n,m)=s(n-1,m) ...

  7. 计算C(n,0)+C(n,1)+...+C(n,m)--Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 四个while的顺序不能变,不知道为什么 #include<bits/stdc++.h> us ...

  8. HDU - 5381 The sum of gcd(莫队/线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,再给出 mmm 次询问,每次询问需要回答区间 [L,R][L,R][L,R] 内所有子区间的 gcdgcdgcd 之和.更具体的,对于询问 ...

  9. hdu 5213(容斥原理+莫队算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5213 莫队算法是离线处理一类区间不修改查询类问题的算法.就是如果你知道了[L,R]的答案.你可以在O( ...

最新文章

  1. 百度CTO王海峰博鳌解读AI“融合创新”,算力算法数据发挥综合作用
  2. spring boot学习资料以及DEMO项目
  3. matlab滤波仿真
  4. 强化学习(五)用时序差分法(TD)求解
  5. 腾讯天津数据中心余热回收应用初探
  6. LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)
  7. liferay spring mvc 案例地址
  8. 自定义smokeping告警(邮件+短信)
  9. View 绘制体系知识梳理(4) 绘制过程之 Layout 详解
  10. windows 安装php7.4并配置phpstorm环境
  11. JS获取页面 GET 方式请求的参数
  12. (转)用Javascript获取页面元素的位置
  13. ../bin/testCurveFitting 出现的错误以及解决办法
  14. QuartusII-项目工程的功能仿真
  15. 【材料计算】输入文件INCAR
  16. 五色电阻在线计算机,色环电阻(5色环在线电阻计算器)
  17. 小白的网站seo经验
  18. 直通车点击率、点击率、创意图、关键词、出价卡位,提升直通车点击率的技巧和方法
  19. 算法小讲堂之B树和B+树(浅谈)|考研笔记
  20. 评价类模型——层次分析法

热门文章

  1. 修改elementUI中el-date-picker内置样式
  2. 【Java】2022年团体程序设计天梯赛 L1 和 L2-042 题解
  3. VBA,把excel单元格复制成图片
  4. 使用Office UI fabric react icons显示图标
  5. Vue3.0基础教程:条件渲染:v-if v-else-if v-if;列表渲染:v-for
  6. 基于PyQt5实现界面控件自适应大小
  7. Cyanosis Observation Index (COI) 发绀观察指数 计算软件
  8. Qt 生成pdf文件
  9. es如何修改es索引字段类型 reindex
  10. 史上最糟糕简历:长度超过一页 使用过多术语