传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6333

题意:

T次询问,每次询问n个苹果中最多拿m个苹果的方法数

题解:

因为T为1e5,所以直接做时间复杂度会很高,所以我们因为每次询问可以离线下来,我们考虑莫队算法

首先这个题可以看作求

\[\sum_{i=0}^mC_n^i\\
令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}
\]

根据这个公式我们就可以用莫队

\[T\sqrt {len}就过了
\]

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ 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 莫队+组合数学的更多相关文章

  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. HDU6333 莫队+组合数

    题目大意: 给定n m 在n个数中最多选择m个的所有方案 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3 ...

  3. 2018.10.23 NOIP训练 Leo的组合数问题(组合数学+莫队)

    传送门 好题. 考察了莫队和组合数学两个知识板块. 首先需要推出单次已知n,mn,mn,m的答案的式子. 我们令f[i]f[i]f[i]表示当前最大值为第iii个数的方案数. 显然iii之后的数都是单 ...

  4. HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...

  5. 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

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

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

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

  8. 20181009noip HZ EZ两校联考sum(莫队,组合数学)

    题面戳这里 思路: noip考莫队???!!! 考场上死活没往这方面想啊!!!数据分治忘写endl50pts滚粗了 这里每个询问都有n,m两个参数 我们可以把它看做常规莫队中的l和r 然后利用组合数的 ...

  9. 「10.11」chess(DP,组合数学)·array(单调栈)·ants(莫队,并茶几)

    菜鸡wwb因为想不出口胡题所以来写题解了 A. chess 昨天晚上考试,有点困 开考先花五分钟扫了一边题,好开始肝$T1$ 看了一眼$m$的范围很大,第一反应矩阵快速幂?? $n$很小,那么可以打$ ...

随机推荐

  1. Oracle安装 卸载 和常见问题

    Oracle的安装   全局数据库名:orcl  口令:orcl 或者以第三方工具SQLplus为例 系统用户:sys 和 system  练习账户:scott (密码:tiger) 登录账户为:sy ...

  2. Extended Traffic

    题目链接 题意:有n个路口,m条通路,如果经过一条路则会得到(终点 - 起点)^3的权值,求从1点到其他点的最小权值,如果权值小于3或无法到达输出'?'. 题解:因为权值可能为负,所以用SPFA来解题 ...

  3. jQuery 鼠标移入图片 显示大图并跟随鼠标移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 在JS中模拟表单的post提交,进行页面的跳转

    原文链接:https://blog.csdn.net/jal517486222/article/details/83147761 /* *功能: 模拟form表单的提交 *参数: URL 跳转地址 P ...

  5. LightOJ 1370 Bi-shoe and Phi-shoe【欧拉函数 && 质数】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题意: 给定值,求满足欧拉值大于等于这个 ...

  6. jQuery学习笔记之可见性过滤选择器

    可见性过滤选择器是根据元素的可见和不可见状态来选择相应的元素. 显示隐藏的例子: <!DOCTYPE html> <html> <head> <script ...

  7. HZOJ Drink

    神仙题,打了个whs式暴力卡常卡A了(我没脸),正解还是要打的,然而作者的题解看不懂…… Drink: 看惯了罗马音的小朋友们都会知道r发l的音,题目名:D Link. 每次修改都会改变O( N ^  ...

  8. js+canvas黑白棋

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 《C语言深度解剖》学习笔记之关键字

    第一章 关键字 C语言共有32个关键字. 关键字   auto 声明自动变量 int 声明整型变量 long 声明长整型变量 char 声明字符型变量 float 声明浮点型变量 short 声明短整 ...

  10. uni-app原生导航栏使用iconfont图标

    在 iconfont 将图标下载之后,会有一个 .ttf 后缀的文件 把它放进 static 文件夹里 然后打开在iconfont下载的  demo_index.html  文件 选择 Unicode ...