任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333

Problem B. Harvest of Apples

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4043    Accepted Submission(s): 1560

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

题意概括:

有 N 个苹果,问最多选 m 个苹果的方案有多少种?

解题思路:

大佬讲的很好了。

https://blog.csdn.net/codeswarrior/article/details/81359075

推出四个公式,算是一道比较裸的莫队了。

Sm−1n=Smn−CmnSnm−1=Snm−Cnm
Sm+1n=Smn+Cm+1nSnm+1=Snm+Cnm+1(或者Smn=Sm−1n+CmnSnm=Snm−1+Cnm)

Smn+1=2Smn−CmnSn+1m=2Snm−Cnm
Smn−1=Smn+Cmn−12Sn−1m=Snm+Cn−1m2(或者Smn=Smn+1+Cmn2Snm=Sn+1m+Cnm2)

需要注意的点较多:

1、精度问题,注意数据范围

2、为了保证精度,除法需要转换为逆元,预处理逆元的技巧

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = 1e5+;
LL N, M;
LL fac[MAXN], inv[MAXN];
struct Query
{
int L, R, id, block;
bool operator < (const Query &p)const{
if(block == p.block) return R < p.R;
return block < p.block;
}
}Q[MAXN];
LL res, rev2;
LL ans[MAXN]; LL q_pow(LL a, LL b)
{
LL pans = 1LL;
while(b){
if(b&) pans = pans*a%MOD;
b>>=1LL;
a = a*a%MOD;
}
return pans;
} LL C(int n, int k)
{
return fac[n]*inv[k]%MOD*inv[n-k]%MOD;
} void init()
{
rev2 = q_pow(, MOD-); // 2的逆元
fac[] = fac[] = ;
for(LL i = ; i < MAXN; i++){ //预处理阶乘
fac[i] = fac[i-]*i%MOD;
} inv[MAXN-] = q_pow(fac[MAXN-], MOD-); //逆推预处理阶乘的逆元
for(int i = MAXN-; i >= ; i--){
inv[i] = inv[i+]*(i+)%MOD;
}
} void addN(int posL, int posR)
{
res = (*res%MOD-C(posL-, posR)%MOD + MOD)%MOD;
} void addM(int posL, int posR)
{
res = (res+C(posL, posR))%MOD;
} void delN(int posL, int posR)
{
res = (res+C(posL-, posR))%MOD*rev2%MOD;
} void delM(int posL, int posR)
{
res = (res - C(posL, posR) + MOD)%MOD;
} int main()
{
int T_case;
init();
int len = (int)sqrt(MAXN*1.0);
scanf("%d", &T_case);
for(int i = ; i <= T_case; i++){
scanf("%d%d", &Q[i].L, &Q[i].R);
Q[i].id = i;
Q[i].block = Q[i].L/len;
}
sort(Q+, Q++T_case);
res = ;
int curL = , curR = ;
for(int i = ; i <= T_case; 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_case; i++){
printf("%lld\n", ans[i]);
} return ; }

2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】的更多相关文章

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

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

  3. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

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

  5. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  6. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  7. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  8. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  9. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

随机推荐

  1. Linux 上安装Docker 并部署netcor2.1

    述 容器,顾名思义是用来存放并容纳东西的器皿: 而容器技术伴着Docker的兴起也渐渐的映入大家的眼帘,它是一个抽象的概念,同时也是默默存在世上多年的技术,不仅能使应用程序间完全的隔离,而且还能在共享 ...

  2. JMS - 基于JMS的RPC

    现在试试通过JMS,在应用程序之间发送消息.先看看spring提供的RPC方案(其实还有其他方案,只是没见过谁用).需要使用到这两个类:·org.springframework.jms.remotin ...

  3. Java - 生成keystore

    有个需求,说要在生成PDF文件时加上signature.操作PDF容易,用: <dependency> <groupId>com.itextpdf</groupId> ...

  4. Nginx - 简易图片服务器

    安装 主要使用Nginx和vsftpd. 安装方面可以直接从nginx官网上下载,或者... yum install nginx 如果没有yum源则需要自行添加再进行install. yum inst ...

  5. 编写简单的maven插件

    编写一个简单的输出maven的hello world的插件 1.在eclipse中新建一个maven project项目 然后取名HelloPlugin,建立后,pom文件为(注意packaging为 ...

  6. JavaScript的进阶之路(二)函数简介,变量、作用域和内存问题

    <h3>ECMAScript中函数不存在函数签名的概念,没有重载</h3><h3>无需指定返回值,可以在任何时候返回任何值.未指定返回值的函数,返回的是一个特殊的u ...

  7. Java 使用Log4J进行日志操作

    使用Log4J进行日志操作   Log4J简介   Log4J是Apache的一个开放源代码项目,它是一个日志操作包,通过使用Log4J,可以指定日志信息输出的目的地,如控制台.文件.CUI组件.NT ...

  8. create-react-app找不到配置项

    npm run eject 这个一个不可逆过程,一旦你执行了,就不能回到初始化

  9. Linux基础之命令练习Day1-init,who,date,cal,man,clear,passwd,su,whoami,mkdir,touch,rm,cp,mv,head,tail,more,less,echo

    开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 使用命令退出虚拟终端2上登录的用户 使用快捷键切 ...

  10. Failed to create the part's controls解决方法

    今早打开eclipse,所有的文件均打不开,如下所示: 因为项目从tomcat迁移到weblogic,JDK版本从1.8降到了1.6,EL表达式有些也解析不了,猜想是这其中出现了问题. 解决方法: F ...