HDU-6333

题意:

有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了。

思路:

这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的方法总数,显然是C(n,0),C(n,1)……C(n,m)的和。

显然S(n,m+1) = S(n, m) + C(n,m+1);

还有一个等式就不那么明显了,S(n+1,m) = 2 * S(n,m) - C(n,m);

我也是在王神犇的指导下明白的。

既然知道了一组(n,m)是可以在很快的时间下转移到(n+1,m),(n-1,m),(n,m+1),(n,m-1)的,这个时候就要想到莫队。把每一组的n和m转化成区间的右端点和左端点,是不是很神奇。

那如何求组合数C(n,m)?可以先预处理出n的前缀阶乘,每次除一下,就可以得到,当然,因为是取模意义下,这里除一下就要去乘以这个数的逆元。

这题还有个细节就是要先更新作为n的右端点,为了防止右端点小于左端点的情况出现,即n 比 m 小。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <iostream>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
// #define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; // template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/
#define bel(x) ((x-1)/B+1)
const int maxn = 1e5+;
const int B = ;
const int MOD = 1e9+;
ll ans[maxn];
struct node {
int n,m;
int id;
} p[maxn]; ll X,Y;
void exgcd(ll a,ll b){
if(b==){
X = ;Y = ;
return;
}
exgcd(b,a%b);
ll tmp = X;
X = Y;
Y = tmp - a/b*Y;
} bool cmp(const node &a,const node &b){
if(bel(a.m) == bel(b.m))
return a.n < b.n;
return bel(a.m) < bel(b.m);
}
ll pm[maxn],TWO,NY[maxn]; //NY是预处理的逆元,不出来会TLE
void init(){
pm[] = ;
exgcd(,MOD);
NY[] = (X + MOD)%MOD;
for(int i=; i<maxn; i++){
pm[i] = (pm[i-] * i + MOD) % MOD;
exgcd(pm[i],MOD);
NY[i] = (X + MOD)%MOD;
} exgcd(,MOD);
TWO = (X + MOD)%MOD;
}
ll get(int n, int x){
if(n-x < )return ;
ll res = (pm[n] *NY[n-x])%MOD;
res = (res * NY[x])%MOD;
return res;
}
ll sum = ;
void del1(int x,int n){
sum =(sum- get(n,x)+MOD)%MOD;
} void add1(int x,int n){
sum=(sum + get(n,x) +MOD)%MOD;
} void del2(int x,int n){
sum = ((sum + get(n,x))*TWO + MOD)%MOD;
} void add2(int x,int n){
sum = (sum * - get(n,x)+MOD)%MOD;
} int main(){
init();
int q;
scanf("%d", &q);
for(int i=; i<=q; i++){
scanf("%d%d",&p[i].n,&p[i].m);
p[i].id = i;
}
sort(p+,p++q,cmp);
int pl = p[].m, pr = p[].n;
for(int i=; i<=pl; i++)
{
sum = (sum + get(pr,i) + MOD)%MOD;
}
ans[p[].id] = sum;
// cout<<"**"<<endl;
for(int i=; i<=q; i++){
while(pr < p[i].n) add2(pl,pr),pr++;//这里要先更新作为n的右区间,防止m>n;
while(pr > p[i].n) pr--,del2(pl,pr); while(pl < p[i].m) pl++,add1(pl,pr);
while(pl > p[i].m) del1(pl,pr),pl--;
ans[p[i].id] = sum%MOD;
} for(int i=; i<=q; i++){
printf("%lld\n", ans[i]);
}
return ;
}

HDU6333

HDU-6333 Problem B. Harvest of Apples 莫队的更多相关文章

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

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

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

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

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

  7. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  8. 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数值范围太 ...

  9. 热身训练1 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 题意: 求 C(0,n)+C(1,n)+...+C(m,n) 分析: 这道题,我们令s(m,n) = C( ...

随机推荐

  1. LinkedHashMap的特殊之处

    一.前言 乍眼一看会怀疑或者问LinkedHashMap与HashMap有什么区别? 它有什么与众不同之处?  由于前面已经有两篇文章分析了HashMap,今天就看看LinkedHashMap.(基于 ...

  2. export,export default,module.exports,import,require之间的区别和关联

    module.exports Node 应用由模块组成,采用 CommonJS 模块规范.根据这个规范,每个文件就是一个模块,有自己的作用域.在这些文件里面定义的变量.函数.类,都是私有的,对外不可见 ...

  3. java中线程安全,线程死锁,线程通信快速入门

    一:多线程安全问题 ###1 引入 /* * 多线程并发访问同一个数据资源 * 3个线程,对一个票资源,出售 */ public class ThreadDemo { public static vo ...

  4. Netty学习(十)-Netty文件上传

    今天我们来完成一个使用netty进行文件传输的任务.在实际项目中,文件传输通常采用FTP或者HTTP附件的方式.事实上通过TCP Socket+File的方式进行文件传输也有一定的应用场景,尽管不是主 ...

  5. 给你的SpringBoot做埋点监控--JVM应用度量框架Micrometer

    JVM应用度量框架Micrometer实战 前提 spring-actuator做度量统计收集,使用Prometheus(普罗米修斯)进行数据收集,Grafana(增强ui)进行数据展示,用于监控生成 ...

  6. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. Java并发之内存模型(JMM)浅析

    背景 学习Java并发编程,JMM是绕不过的槛.在Java规范里面指出了JMM是一个比较开拓性的尝试,是一种试图定义一个一致的.跨平台的内存模型.JMM的最初目的,就是为了能够支多线程程序设计的,每个 ...

  8. Console也要美颜了,来给Console添色彩

    我们在开发过程中,经常需要将不同的信息用颜色标记出来,这可以让我们快速关注到重点信息.想必大家都知道,可以通过Console. ForegroundColor设置输出文字的颜色,背景颜色可以通过Con ...

  9. bs4-BeautifulSoup

    1.BeautifulSoup下载 pip install BeautifulSoup4 或者 pip install bs4 pip install lxml #解析器 2.BeautifulSou ...

  10. Mina实现Socket通信完整过程

    目录 服务端 客户端 通信 自定义工厂编解码 解码器 编码器 总结 # 加入战队 微信公众号 title: Mina服务端客户端通信 date: 2018-09-30 09:00:30 tags: - ...