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/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4043 Accepted Submission(s): 1560
Count the number of ways to pick at most m apples.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
题意概括:
有 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 【莫队+排列组合+逆元预处理技巧】的更多相关文章
- 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 (莫队+组合数学)
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 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) 设 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Centos时间查看修改命令date详解
1.查看.修改Linux时区与时间 一.linux时区的查看与修改 1,查看当前时区date -R 2,修改设置时区方法1:tzselect 方法2:仅限于RedHat Linux 和 CentOSt ...
- git flow强制重新初始化
Gitflow工作流定义了一个围绕项目发布的严格分支模型. git flow初始化命令: git flow init 关于各个分支的命名一路回车就可以了,如果不小心修改了默认的分支命名,后来又觉得不爽 ...
- 一:Maven知识整理
一:maven的好处 1.依赖管理:对jar包的统一管理 可以节省空间 2.项目一键构建: 编码 编译 测试(junit) 运行 打包 部署 一个 tomcat:run就能把项目运行起来 Maven能 ...
- spring-boot-maven-plugin 插件
添加了spring-boot-maven-plugin插件后,当运行maven打包的命令,项目会被打包成一个可以直接运行的jar包,使用"java -jar"可以直接运行. 当项目 ...
- JavaScript之如何对客户端进行检测
本文主要是针对各种客户端进行检测,使用了用户代理字符串检测技术,具体代码如下: var client=function() { var engine= { // 呈现引擎 ie: 0, gecko: ...
- hdu 1011 Starship Troopers 经典的树形DP ****
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 最新版PMBOK项目管理的五大过程组和十大知识领域
PMBOK五大过程组是:启动过程.规划过程.执行过程.监控过程.收尾过程. 各用一句话概括项目管理知识体系五大过程组: 1.启动过程组:作用是设定项目目标,让项目团队有事可做: 2.规划过程组:作用是 ...
- The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)
1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: in the jsp file: /WEB-INF/pag ...
- jquery插件一直报错:xx is not a function
当然像js文件未引用或者js插件使用方法不对这样的解决办法想必大家都已经试过了. 那么在放弃前请最后看一下是不是引用了两个jquery文件. 引用了两个jquery文件! 引用了两个jquery文件! ...
- git 命令收藏
git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config -- ...