HDU6333 莫队+组合数学
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6333
题意:
T次询问,每次询问n个苹果中最多拿m个苹果的方法数
题解:
因为T为1e5,所以直接做时间复杂度会很高,所以我们因为每次询问可以离线下来,我们考虑莫队算法
首先这个题可以看作求
令S(n,m)为\sum_{i=0}^mC_n^i\\
可以得到公式\\
S(n,m)=S(n,m-1)+C_n^m\\
S(n,m)=S(n,m=1)-C_n^m+1\\
S(n,m)=2*S(n-1,m)-C_{n-1}^m\\
S(n,m)=\frac{S(n+1,m)+C_n^m}{2}
\]
根据这个公式我们就可以用莫队
\]
代码:
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
struct node {
int l, r, id;
} q[maxn];
int pos[maxn];
bool cmp(node a, node b) {
if(pos[a. l] == pos[b.l]) return a.r < b.r;
return pos[a.l] < pos[b.l];
}
LL Ans;
LL f[maxn], inv[maxn];
LL qpow(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() {
f[1] = 1;
for (int i = 2; i < maxn; i++) f[i] = (f[i - 1] * i) % mod;
inv[maxn - 1] = qpow(f[maxn - 1], mod - 2);
for (int i = maxn - 2; i >= 1; i--) inv[i] = (inv[i + 1] * (i + 1)) % mod;
}
LL C(int n, int m) {
if (n < 0 || m < 0 || m > n) return 0;
if (m == 0 || m == n) return 1;
return f[n] * inv[n - m] % mod * inv[m] % mod;
}
LL ans[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
init();
int n;
scanf("%d", &n);
int sz = sqrt(n);
for(int i = 1; i <= n; i++) {
pos[i] = i / sz;
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q + 1, q + n + 1, cmp);
int l = 1, r = 0;
Ans = 1;
for(int i = 1; i <= n; i++) {
while(l < q[i].l) {
Ans = Ans * 2 % mod - C(l, r);
if (Ans < 0) Ans += mod;
l++;
}
while(l > q[i].l) {
l--;
Ans = (Ans + C(l, r)) % mod * inv[2] % mod;
}
while(r < q[i].r) {
r++;
Ans = (Ans + C(l, r)) % mod;
}
while(r > q[i].r) {
Ans = (Ans - C(l, r) + mod) % mod;
r--;
}
ans[q[i].id] = Ans;
}
for(int i = 1; i <= n; i++) {
printf("%lld\n", ans[i]);
}
return 0;
}
HDU6333 莫队+组合数学的更多相关文章
- 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)).有公 ...
- HDU6333 莫队+组合数
题目大意: 给定n m 在n个数中最多选择m个的所有方案 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3 ...
- 2018.10.23 NOIP训练 Leo的组合数问题(组合数学+莫队)
传送门 好题. 考察了莫队和组合数学两个知识板块. 首先需要推出单次已知n,mn,mn,m的答案的式子. 我们令f[i]f[i]f[i]表示当前最大值为第iii个数的方案数. 显然iii之后的数都是单 ...
- HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...
- HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))
2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- 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) 设 ...
- 20181009noip HZ EZ两校联考sum(莫队,组合数学)
题面戳这里 思路: noip考莫队???!!! 考场上死活没往这方面想啊!!!数据分治忘写endl50pts滚粗了 这里每个询问都有n,m两个参数 我们可以把它看做常规莫队中的l和r 然后利用组合数的 ...
- 「10.11」chess(DP,组合数学)·array(单调栈)·ants(莫队,并茶几)
菜鸡wwb因为想不出口胡题所以来写题解了 A. chess 昨天晚上考试,有点困 开考先花五分钟扫了一边题,好开始肝$T1$ 看了一眼$m$的范围很大,第一反应矩阵快速幂?? $n$很小,那么可以打$ ...
随机推荐
- Servlet工作流程
1.加载Servlet类 类加载器负责加载servlet类. 当Web容器接收到servlet的第一个请求时,将加载servlet类. 2.创建Servlet实例 Web容器在加载servlet类之后 ...
- SpringMVC的简单传值总结
之前学习SpringMVC时感觉他的传值很神奇:简便,快捷,高效. 今天写几个简单的传值与大家分享,希望能对大家有帮助. 一. 从后往前传: (1) @Controller @RequestMappi ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第一章:向量代数 学习目标: 学习如何使用几何学和数字描述 Vecto ...
- QT_OPENGL-------- 4.可编程管线绘制三角形
一.环境:qt下qmake编译首先在qt .pro文件中添加glew和glfw的链接 LIBS+= -L/usr/lib64 -lGLEW LIBS +=-L/usr/local/lib -lglfw ...
- 2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci)
2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci) 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 找规律 找两个节点的lca,需 ...
- SQLServer一条SQL查出当月的每一天
from master..spt_values ),,),'2013-02-03')+'-01' as datetime)) 结果: 返回带有年月日的日期 ),),) AS datetime) fro ...
- 模板—插头dp(Ural 1519 Formula 1)
括号表示法: 据说比下一个要快而且灵活. #include<iostream> #include<cstring> #include<cstdio> #define ...
- C运行时库函数
C运行时库函数是指C语言本省支持的一些基本函数,通常是汇编直接实现的. API函数是操作系统提供给用户方便设计应用程序的函数,实现一些特定的功能,API函数也是C语言的函数实现的. 他们之间区别是: ...
- laravel5.4 前后台未登陆,跳转到各自的页面
https://www.imooc.com/wenda/detail/378208?t=266634 laravel我做了前后台登陆,后台未登录跳转到前台登陆页面了. 我想让后台未登入跳转到后台登陆页 ...
- jasperReports 通过java导出各种格式报表,及javaweb项目整合显示各种报表
注意:编写此项目时,请先手动编写**.jrxml 或者用IReport生成**.jrxml或**.jasper 注:java导出报表与web项目显示报表方法其实是相同的,只不过有一句代码不同,如下 ...