hdu多校第4场 B Harvest of Apples(莫队)
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(莫队)的更多相关文章
- 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)).有公 ...
- 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 ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU 多校第四场题解
对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- HDU多校训练第一场 1012 Sequence
题目链接:acm.hdu.edu.cn/showproblem.php?pid=6589 题意:给出一个长度为n的数组,有m次操作,操作有3种1,2,3,问操作m次后的数组,输出i*a[i]的异或和 ...
随机推荐
- GIL学习
GIL锁 一.GIL的简单概述 二.GIL对于多线程的影响 三.解决GIL对于多线程影响的方案 回到顶部 一.GIL的简单概述 1.概念 GIL ( Global Interperter Lock ) ...
- (转)Linux vi 命令大全
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...
- ubuntu 安装完后对于开发需要做的事情
是从 https://www.osboxes.org/ubuntu/ 下载的vdi文件,估计vmware对应的应该也有. 1. 安装 openssh-server apt-get install op ...
- python模拟面试技术题答案
目录 Python4期模拟面试技术面试题答案............................................................................ ...
- docker overlay
http://blog.csdn.net/jiangshouzhuang/article/details/52822125
- 外网访问ARM嵌入式Linux系统
外网访问ARM嵌入式Linux系统 实验室里的ARM嵌入式Linux系统,只能在局域网内访问,怎样从外网也能访问ARM嵌入式Linux系统? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并 ...
- Java 文件重命名
Java 文件重命名 /** * 重命名文件 * @param fileName * @return */ public static void renameFile(String filePath, ...
- oracle查询buffer cache中undo大小
1.Does undo buffer exists or changes will directly write to undo datafiles? Undo blocks are database ...
- [Python]基础教程(1)、介绍及环境搭建
一.Python简介 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言,是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 是一种解释型语言: 这意味着开 ...
- 剑指offer(32)把数组排成最小的数
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 题目分析 主 ...