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≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
 
Output
For each test case, print an integer representing the number of ways modulo 109+7.
 
Sample Input
2
5 2
1000 500
 
Sample Output
16
924129523
 
Source
 

解析:

   

  

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
const int MOD = 1e9+;
LL n, m, ans;
LL up[maxn], down[maxn], pos[maxn], inc[maxn], inv[maxn]; struct node
{
LL l, r;
int id;
}Node[maxn]; bool cmp(node a, node b)
{
return pos[a.l] == pos[b.l] ? (a.r < b.r) : (a.l < b.l);
} LL qp(LL a, LL b)
{
LL res = ;
while(b)
{
if(b & ) res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} void init()
{
up[] = ;
down[] = ;
for(int i=; i<maxn; i++)
{
up[i] = up[i-] * i % MOD;
down[i] = qp(up[i], MOD - ) % MOD;
}
} LL C(LL n, LL m)
{
if(n < m) return ;
return up[n] * down[n-m] % MOD * down[m] % MOD;
} int main()
{
init();
int block = sqrt();
for(int i=; i<=; i++)
pos[i] = (i-)/block + ;
int T;
rd(T);
for(int i=; i<=T; i++)
{
rlld(Node[i].r), rlld(Node[i].l);
Node[i].id = i;
}
sort(Node + , Node + T + , cmp);
ans = ;
int tmp = qp(, MOD - );
for(int i=, l=, r=; i<=T; i++)
{
for(; r < Node[i].r; r++)
ans = ( * ans - C(r, l) + MOD) % MOD;
for(; r > Node[i].r; r--)
ans = (ans + C(r-, l)) * tmp % MOD;
for(; l < Node[i].l; l++)
ans = (ans + C(r, l+)) % MOD;
for(; l > Node[i].l; l--)
ans = (ans - C(r, l) + MOD) % MOD;
if(Node[i].l == Node[i].r)
{
inc[Node[i].id] = ;
} inc[Node[i].id] = ans;
}
for(int i=; i<=T; i++)
printf("%lld\n", inc[i]); return ;
}

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

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

  2. HDU 6333 莫队+组合数

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

  3. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

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

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

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

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

  7. HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)

    题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...

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

  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. [05] Bean的作用域和生命周期

    1.Bean的作用域和初始化时间 之前我们稍微提到过,Spring中管理的Bean,默认都是单例模式,这意味着你多次获取某个对象,得到的都是相同的对象.单例作用域的显性写法是scope属性,如下,这和 ...

  2. 盖茨基金会:如何使用Python拯救生命

    每年全球都要花费数十亿美元来预防疾病,减少死亡,资助预防保健及治疗的各种研发项目,以及其他的健康方案.但资金毕竟是有限的,所以一些组织,比如全球卫生资金的主要捐助者比尔&梅林达·盖茨基金会(B ...

  3. 使用第三方库(Senparc)完成小程序支付 - z

    https://www.cnblogs.com/zmaiwxl/p/8931585.html

  4. 深入理解 JS 引擎执行机制(同步执行、异步执行以及同步中的异步执行)

    首先明确两点: 1.JS 执行机制是单线程. 2.JS的Event loop是JS的执行机制,深入了解Event loop,就等于深入了解JS引擎的执行. 单线程执行带来什么问题? 在JS执行中都是单 ...

  5. springboot 中事件监听模型的一种实现

    目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...

  6. php7与之前的区别和更新【转】

    http://blog.csdn.net/u011957758/article/details/73320083 本文是一篇讲座听后+后续研究的总结. 话说当年追时髦,php7一出就给电脑立马装上了, ...

  7. 腾讯QQ的商业模式

    近期听到许多关于腾讯QQ的报道,然后想到之前自己在QQ上遇到的一些问题,一瞬间感觉大脑的所有想法喷涌而出. 以前总觉得QQ是个很好的平台,我们可以通过QQ和自己的亲人朋友爱人聊天,有时候还可以在自己的 ...

  8. java中定时执行任务

    现在项目中用到需要定时去检查文件是否更新的功能.timer正好用于此处. 用法很简单,new一个timer,然后写一个timertask的子类即可. 代码如下: package comz.autoup ...

  9. 第一次Spring总结

    第一阶段:下载了类似app使用,并做了对比,分析,对自己的app有了一些构思,完成了环境的配置.在这一阶段,一开始只有两个女生显得有点弱,面对从未接触过的app项目,首先就是配置环境方面的,在经过班上 ...

  10. Filter(转载)

    web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...