问题 B: Harvest of Apples

时间限制: 1 Sec  内存限制: 128 MB
提交: 18  解决: 11
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

输入

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

输出

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

样例输入

2
5 2
1000 500

样例输出

16
924129523

莫队(分块),组合数学。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int mod=1e9+;
ll fac[maxn],inv[maxn],ans[maxn];
ll rev2,res;
int pos[maxn];
ll qpow(ll b,int n)
{
ll res=;
while(n)
{
if(n&) res=res*b%mod;
b=b*b%mod;
n>>=;
}
return res;
}
ll Comb(int n,int k)
{
return fac[n]*inv[k]%mod*inv[n-k]%mod;
}
void pre()
{
rev2=qpow(,mod-);
fac[]=fac[]=;
for(int i=; i<maxn; ++i) fac[i]=i*fac[i-]%mod;
inv[maxn-]=qpow(fac[maxn-],mod-);
for(int i=maxn-;i>=;i--) inv[i]=inv[i+]*(i+)%mod;
}
struct Query
{
int L,R,id;
bool operator <(const Query& p) const
{
if(pos[L]==pos[p.L]) return R<p.R;
return L<p.L;
}
} Q[maxn];
inline void addN(int posL,int posR)
{
res=(*res%mod-Comb(posL-,posR)+mod)%mod;
}
inline void addM(int posL,int posR)
{
res=(res+Comb(posL,posR))%mod;
}
inline void delN(int posL,int posR)
{
res=(res+Comb(posL-,posR))%mod*rev2%mod;
}
inline void delM(int posL,int posR)
{
res=(res-Comb(posL,posR)+mod)%mod;
}
int main()
{
int T,curL,curR;
int block=(int)sqrt(1.0*maxn);
pre();
scanf("%d",&T);
for(int i=;i<=T;++i)
{
scanf("%d %d",&Q[i].L,&Q[i].R);
pos[i]=i/block;
Q[i].id=i;
}
sort(Q+,Q+T+);
res=;
curL=,curR=;
for(int i=;i<=T;++i)
{
while(curL<Q[i].L) addN(++curL,curR);
while(curR<Q[i].R) addM(curL,++curR);
while(curL>Q[i].L) delN(curL--,curR);
while(curR>Q[i].R) delM(curL,curR--);
ans[Q[i].id] = res;
}
for(int i=;i<=T;++i) printf("%lld\n",ans[i]);
return ;
}

Harvest of Apples的更多相关文章

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

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

  2. HDU 6333 Harvest of Apples (分块、数论)

    题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...

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

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

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

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

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

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

  8. HDU - 6333:Harvest of Apples (组合数前缀和&莫队)

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

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

随机推荐

  1. ]Linq to EF 增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. CentOS7 环境下MySQL5.7 PHP7的安装

    用的都是最新版的包 : php7 mysql5.7 nginx1.8  zabbix3.2.6 替换源为阿里云的源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/ ...

  3. SPFA(热浪)

    1557 热浪  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果  

  4. 取消eclipse英文单词拼写验证

    依次点击Preferences->General->Editors->TexEditors->Spelling->Enable Spell Checkingt , 如下图 ...

  5. 10.Python初窥门径(函数进阶)

    Python(函数进阶) 一.函数的传参(接上期) 形参角度(一共四种,后两种) 动态参数(万能参数)* # 定义一个函数时,*所有的位置参数聚合到一个元组中 def func(*args): # * ...

  6. 基于SSM搭建网站实现增删改查

    网站源码地址:https://github.com/MyCreazy/BasicOperateWebSite.git 使用maven搭建网站的时候,记得选用war包格式,有时候maven包没有引用进来 ...

  7. Python-15-收集参数

    允许用户提供任意数量的参数: def print_params(*params): print(params)   >>> print_params('Testing') ('Tes ...

  8. PAT刷题记录

    1. 1007 素数对猜想 :运行超时 算法问题还是语言本身效率低下? import math def is_prime(num): if num == 2: return num temp = in ...

  9. 微信小程序入门文档

    一 基本介绍 微信专门为小程序开发了一个ide叫做微信开发者工具 最新一版的微信开发者工具,把微信公众号的调试开发工作也集成了进去,可以更换开发模式. https://mp.weixin.qq.com ...

  10. Table 自定义设置边框线

    table上添加:border-collapse: collapse;//设置表格边框分开显示就可以设置tr td的边框线