hdu6333 Problem B. Harvest of Apples(组合数+莫队)
hdu6333 Problem B. Harvest of Apples
题意:
求(0,n)~(m,n)组合数之和
题解:

代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int mod=1e9+;
const int N=1e5+; /********组合数模板*********/
ll pow_mod(ll a, ll b) {
ll res = 1ll; a %= mod;
while(b){
if(b & ) res = res * a % mod;
a = a * a % mod;
b >>= ;
} return res;
}
ll inv(ll a) { return pow_mod(a, mod-); }
ll F[N], Finv[N];//F是阶乘,Finv是逆元的阶乘
void init() {
F[] = Finv[] = 1ll;
for(int i = ; i < N; i ++){
F[i] = F[i-] * 1ll * (ll)i % mod;
Finv[i] = Finv[i-] * 1LL * inv(i) % mod;
}
} // O(n)预处理
ll C(ll n, ll m) {
if(m < || m > n) return ;
return F[n] * 1LL * Finv[n - m] % mod * Finv[m] % mod;
} // O(1)获得组合数C(n,m)
/**************************/ int block[N];
ll res[N];
struct mo
{
int n,m;
int id,block;
bool operator < (const mo &p) const{
if(block==p.block) return n<p.n;
else return block<p.block;
}
}s[N];
int main()
{
int T;
scanf("%d",&T);
int len=sqrt(N+0.5);
for(int i=;i<T;i++)
{
scanf("%d %d",&s[i].n,&s[i].m);
s[i].block=s[i].m/len;s[i].id=i;
}
sort(s,s+T);
ll ans=;
init();
int L=,R=;
for(int i=;i<T;i++)
{
int l=s[i].n,r=s[i].m;
while(L>l) ans=((ans+C(L-1LL,R))%mod*Finv[])%mod, L--;
while(L<l) ans=(*ans%mod-C(L,R)+mod)%mod, L++;
while(R<r) ans=(ans+C(L,R+))%mod, R++;
while(R>r) ans=(ans-C(L,R)+mod)%mod, R--;
res[s[i].id]=ans;
}
for(int i=;i<T;i++)
printf("%lld\n",res[i]);
return ;
}
hdu6333 Problem B. Harvest of Apples(组合数+莫队)的更多相关文章
- 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/200 ...
- HDU - 6333 Problem B. Harvest of Apples (莫队)
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...
- 【HDOJ6333】Harvest of Apples(莫队)
题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...
- Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±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 ...
随机推荐
- “没有找到mfc100u.dll”的解决方法
现在需要安装 MindManager 2016 思维导图软件时,打开软件提示找不到 mfc100u.dll,无法执行程序.之前一直好好的,现在换电脑了安装提示这个问题,然后百度找的解决方案: 需要去微 ...
- Linux--shell交互输入与循环语句--06
一.交互输入 1.命令用法:read a b c -> aa bb cc read命令同时可以定义多个变量值:而输入的内容默认以空格为分隔符,将值输入到对应的变量中:如果默认值输入过多,最后 ...
- WEB服务动静结合
基本介绍 1)WEB服务仅能处理静态请求,如果处理动态请求则需要对应的动态资源服务软件,即:应用程序服务软件 2)常见的应用服务软件有:PHP.Java.Python等 3)问题:WEB服务如何与外部 ...
- less:匹配模式
相当于JS中的if(不完全是),满足条件后才能匹配 .margin(top, @width: 5px) { margin: @width 0 0 0; } .margin(right, @width: ...
- 解决nginx proxy_pass反向代理cookie,session丢失的问题
参考地址:https://www.jianshu.com/p/34abe7eb6f0b 为什么cookie 会丢失? 比如说一个没有经过代理的地址 : http://127.0.0.1/project ...
- alert(1) to win 2
function escape(s) { s = s.replace(/"/g, '\\"'); return '<script>console.log("' ...
- [luogu]P1041 传染病控制[dfs]
[luogu]P1041 传染病控制 ——!x^n+y^n=z^n 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的 ...
- 十一、atomic、async深入
一.原子操作 g++; g+=1; g = g+1;//结果不对 一般原子操作针对++,--,+=,&=,|=,^=是支持的,其他的可能不支持 二.std::async深入 用来创建异步任务. ...
- php面试专题---19、MySQL高可扩展和高可用考点
php面试专题---19.MySQL高可扩展和高可用考点 一.总结 一句话总结: 要区别分区和分库分表,分区的话对用户是透明的,分库分表的话需要程序员做点事情,主从数据库同步的话借助的是二进制日志 1 ...
- jmeter常用组件简介
本文主要介绍jmeter使用过程中最常用的一些组件及其作用.性能测试时线程组中可以添加如下的组件,如图所示: test plan:测试计划,是其它组件的容器 thread:线程组,用来设置多少线程,怎 ...