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≤1e5) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤1e5).

Output

For each test case, print an integer representing the number of ways modulo 1e9+7.

Sample Input

2
5 2
1000 500

Sample Output

16
924129523

Source

2018 Multi-University Training Contest 4

题意:让你求    范围:i属于[0,m]     我们把左边式子简称为S(n,m)

2000ms的时限 O(T*m)的复杂度无法按正常的求和做。

所以根据这个题的特性,离线查询,可以使用莫队算法。(打比赛时,QAQ太菜了,学的东西还是太少了)

我们根据组合数的公式,可以推出以下公式:

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

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

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

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

这样我们就可以找到区间S(n+1,m),S(n-1,m),S(n,m+1),S(n,m-1)

酱紫就可以利用莫队分块了

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const int mod = 1e9+7;
struct query{int l;int r;int id;
};
query e[maxn];
ll ans[maxn];
ll inv[maxn],fac[maxn];
int block;//分块数
int pos[maxn];
int curL = 1;
int curR = 1;
ll now;
ll inv2;
ll qpow(ll a,int p)
{ll tmp = 1;while(p){if(1&p) tmp = (tmp*a)%mod;a = (a*a)%mod;p>>=1;}return tmp;
}
void init()
{fac[0] = 1;for(int i=1;i<=maxn;i++)fac[i] = (fac[i-1]*i)%mod;inv[maxn] = qpow(fac[maxn],mod-2);inv2 = qpow(2,mod-2);for(int i=maxn;i>=1;i--)inv[i-1] = (inv[i]*i)%mod;
}
ll comb(int n,int m)
{if(m<0 || m>n) return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
bool cmp(query a,query b)
{return (pos[a.l] == pos[b.l])?a.r<b.r:a.l<b.l;
}
inline void addN(int posL,int posR) //S(n-1,m)时
{now = (2*now-comb(posL-1,posR)+mod)%mod;
}
inline void addM(int posL,int posR)//S(n,m-1)时
{now = (now+comb(posL,posR))%mod;
}
inline void delN(int posL,int posR)//S(n+1,m)时
{now = (now+comb(posL-1,posR))%mod*inv2 %mod;
}
inline void delM(int posL,int posR)//S(n,m+1)时
{now = (now-comb(posL,posR)+mod)%mod;
}
int main()
{#ifdef LOCAL_FILEfreopen("in.txt","r",stdin);#endif // LOCAL_FILEint t;init();block = (int)sqrt(maxn);scanf("%d",&t);for(int i=1;i<=t;i++){scanf("%d %d",&e[i].l,&e[i].r);e[i].id = i;pos[i] = i/block;//**}sort(e+1,e+t+1,cmp);now = 2;//curL = curR = 1;s(1,1) = 2;for(int i=1;i<=t;i++){int L = e[i].l;int R = e[i].r;while(curL<L) addN(++curL,curR);while(curR<R) addM(curL,++curR);while(curL>L) delN(curL--,curR);while(curR>R) delM(curL,curR--);ans[e[i].id] = now;}for(int i=1;i<=t;i++){printf("%I64d\n",ans[i]);}return 0;
}

[HDU](6333)Problem B. Harvest of Apples ---- 数论+莫队算法相关推荐

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

    Problem B. Harvest of Apples(莫队离线) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/26214 ...

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

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

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

  4. hdu 5381 2015多校第八场 莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5381 还没学过莫队算法....网上也找不到莫队算法的论文,只能勉强看着别人的代码打下来... 稍微介绍 ...

  5. HDU - 6333 Harvest of Apples (莫队)

    题目链接 题意:求出C(n,0)~C(n,m)的和,mod 1e9+7 . 分析:题目给的数据范围1<=n,m<=1e5,1<=T<=1e5,直接求出C(n,i)然后累加起来显 ...

  6. [数据结构专训][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 ...

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

  8. hdu 4358(莫队算法+dfs序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 解题思路:用dfs求出整棵树的dfs序列,这样以u为根节点的子树就转化到相对应的区间上了.由于是 ...

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

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

最新文章

  1. h3csnmp管理命令_H3C S5500V2-EI系列以太网交换机 命令参考-Release 1118-6W100_网络管理和监控命令参考_SNMP命令-新华三集团-H3C...
  2. 小米手机与魅族的PK战结果 说明了什么
  3. 感觉灵感被掏空?你需要这 9 篇论文来补一补 | PaperDaily #05
  4. leetcode 227. 基本计算器 II(栈)
  5. 6. 区别值类型和引用类型。
  6. Ubuntu下安装并配置VS Code编译C++
  7. 用indesign怎么更换名牌姓名_北京怎么脱单?来北京相亲会,哪里有单身交友聚会?户外交友活动...
  8. 平面纹理坐标和球面坐标互相转换
  9. bugly怎么读_腾讯Bugly巨坑:使用不当造成UI界面卡死
  10. pandoc实现文档不同格式的转换
  11. 无线智能报警暨家电控制
  12. 283页K8S实战指南,内容详实,代码齐全可复制!
  13. 2021 Java后端+大数据暑期实习大厂面经
  14. ai条码插件免安装_ai cs6条码插件 支持Illustrator cs6的条码生成脚本
  15. IBM DB2基础知识学习作业
  16. 国密算法(SM2,SM3,SM4)辅助工具升级版(OTP+PBOC3.0)
  17. 分布式系统学习共性总结:
  18. android 高性能手机排行榜,2020年9月安兔兔Android旗舰手机性能跑分排行榜前十名推荐榜单...
  19. 【Android】性能测试之获取Android流量数据
  20. windows7系统损坏修复_UEFI?安装纯净的 Windows 7/10 系统

热门文章

  1. 【观察】“互联网+政务服务”提速,迈入数字政府新阶段
  2. C++ primer笔记
  3. Windows系统设置宽带连接开机自动拨号的方法
  4. 证监会:将重点关注公司上市不满三年卖壳行为
  5. 可视化系列讲解:css2.5D动画->帧动画
  6. 贪心算法解旅行家的预算问题
  7. 1824. Minimum Sideway Jumps 贪心和DP方法
  8. 春晚背后的“新技术”,腾讯技术助力央视频春晚“新看法”
  9. 基于RK3399Pro的SARADC数据采集-内存映射
  10. 经典英文爱情电影对白