Problem B. Harvest of Apples

Time Limit: / MS (Java/Others)    Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
There are n apples on a tree, numbered from to n.
Count the number of ways to pick at most m apples. Input
The first line of the input contains an integer T (≤T≤) denoting the number of test cases.
Each test case consists of one line with two integers n,m (≤m≤n≤). Output
For each test case, print an integer representing the number of ways modulo +. Sample Input Sample Output Source
Multi-University Training Contest Recommend
chendu | We have carefully selected several similar problems for you:

求C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

设S(n,m)=C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

第一个式子易得,第二个式子:杨辉三角的 n,m=(n-1,m)+(n-1,m-1)

那么就是这一行等于上一行的都用了2次,只有第最后一个用了一次

所以减去c(n-1,m)

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
#define ll long long
const int maxn=1e5+7;
ll jiecheng[maxn],inv[maxn];
ll ans[maxn];
int block;
ll qsm(ll a,ll b)
{
ll ans=1;
while(b){
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void init()
{
jiecheng[1] = 1;
for(int i = 2; i < maxn; i++)
jiecheng[i] = jiecheng[i-1] * i % mod;
for(int i = 1; i < maxn; i++)
inv[i] = qsm(jiecheng[i], mod-2);
}
struct node{
int l,r;
int i;
}modui[maxn];
bool cmp(node a,node b)
{
if(a.l/block==b.l/block)
return a.r<b.r;
return a.l<b.l;
}
ll C(ll n,ll m)
{ if(m == 0 || m == n) return 1;
ll ans=1;
ans=(jiecheng[n]*inv[m])%mod*inv[n-m];
ans=ans%mod;
return ans;
}
int main()
{
init();
block = sqrt(maxn);
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
scanf("%d%d",&modui[i].l,&modui[i].r);
modui[i].i=i;
}
sort(modui,modui+t,cmp);
int l=1,r=0;
int sum=1;
for(int i = 0; i < t; i++)
{
while(l < modui[i].l) sum = (2 * sum - C(l++, r) + mod) % mod;
while(l > modui[i].l) sum = ((sum + C(--l, r))*inv[2]) % mod;
while(r < modui[i].r) sum = (sum + C(l, ++r)) % mod;
while(r > modui[i].r) sum = (sum - C(l, r--) + mod) % mod;
ans[modui[i].i] = sum;
}
for(int i=0;i<t;i++)
{
printf("%lld\n",ans[i]);
} return 0;
}

hdu多校第4场 B Harvest of Apples(莫队)的更多相关文章

  1. 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)).有公 ...

  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. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  5. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

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

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

  7. HDU 多校第四场题解

    对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...

  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. HDU多校训练第一场 1012 Sequence

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=6589 题意:给出一个长度为n的数组,有m次操作,操作有3种1,2,3,问操作m次后的数组,输出i*a[i]的异或和 ...

随机推荐

  1. git忽略规则.gitignore未生效解决办法

    创建git本地的仓库常用命令:git init #初始化--------生成.git文件 配置本地用户名跟邮箱(这样就知道是谁提交的东西)git config --global user.name [ ...

  2. abap test msg

  3. Parallels Desktop 重装系统

    安装教程,大家可以在网上找找 现在我想重装系统,怎么弄呢? 1.~/Documents/Parallels 目录下那个PVM后缀的文件直接删除 2.重装找开虚拟机,会弹出一个框,说找不到系统,点击删除 ...

  4. asp.net 导出excel--NPOI

    1.使用OLEDB导出Excel ,这种方式有点慢,慎用 /// <summary> /// 使用OLEDB导出Excel /// </summary> /// <par ...

  5. kendo treeview checkbox初始化选中问题,没解决,暂时记录下

    想做带有checkbox的tree,由于项目一直用kendo ui for mvc,感觉 牛逼的kendo肯定有tree.结果碰到了选中的问题. 无法根据后台传来的IsChecked字段来设置  tr ...

  6. Array 转 Set

    Array 转 Set: Set<String> oldCandidateNames = new HashSet<String>(Arrays.asList(candidate ...

  7. URIError: Failed to decode param '/%PUBLIC_URL%/favicon.ico'

    今天搭建antd的项目结构,本来项目是一个基础react项目,结果执行 yarn create umi yarn yarn start 项目启动后访问突然报错URIError: Failed to d ...

  8. Docker bridge-utils 工具简单部署

    bridge-utils 网桥查看工具 # 1.安装 查看桥接工具 yum install -y bridge-utils # 2.查看桥接 命令brctl show bridge name brid ...

  9. SVN的Not authorized to open root of edit operation解决办法

    以为经常用到这是转贴  谢谢 Subversion装了1.5.2版,乌龟SVN装的是1.5.1版本,可以通过乌龟正常访问到版本库,但当check out时却出现了"Not authorize ...

  10. SOAPdenovo组装软件使用记录

    背景: 1.为什么要从头测序组装基因组? 基因组是不同表型的遗传基础:获得参考基因组是深入研究一个生物体全基因组的第一步也是必须的一步:从头测序组装能够对新的测序物种构建参考基因组: 2.为什么要研究 ...