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. JWT token 跨域认证

    JSON Web Token(缩写 JWT),是目前最流行的跨域认证解决方案. session登录认证方案:用户从客户端传递用户名.密码等信息,服务端认证后将信息存储在session中,将sessio ...

  2. 挂起(suspend)与线程阻塞工具类LockSupport

    挂起(suspend)与线程阻塞工具类LockSupport 一般来说是不推荐使用suspend去挂起线程的,因为suspend在导致线程暂停的同时,并不会去释放任何锁资源. 如果其他任何线程想要访问 ...

  3. 9-2、大型项目的接口自动化实践记录----递归判断两个json串是否相等

    1.已知json串构成的情况下判断 先构造一下场景,假设已经把各个数据都移除掉不对比的字段 图1 预期.实际结果,复杂接口返回多层嵌套json时,同下 图2 预期.实际结果值为:{child_json ...

  4. 夯实Java基础(二)——面向对象之封装

    1.封装介绍 封装封装,见名知意,就是把东西包装隐藏起来,不被外界所看见, 而Java特性封装:是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数 ...

  5. 分布式ID系列(3)——数据库自增ID机制适合做分布式ID吗

    数据库自增ID机制原理介绍 在分布式里面,数据库的自增ID机制的主要原理是:数据库自增ID和mysql数据库的replace_into()函数实现的.这里的replace数据库自增ID和mysql数据 ...

  6. MySQL一键生成实体文件的神器-ginbro

    Java转过来的同学对Mybatis的使用肯定不陌生,特别是对一堆表去生成相应的dao和entity的时候使用Mybatis generator所带来的感触,无比深刻.前面我们也讲过原生的数据库使用, ...

  7. JSmooth 将java代码打包成exe

    JSmooth 将java代码打包成exe 前言 java代码写了这么多了,但由于jdk的限制,我只能在jdk电脑上运行项目.所以最近在研究打包exe这个问题,今天终于实现了. JSmooth下载 前 ...

  8. 记一次mysql主从同步因断电产生的不能同步问题 1236 1032

    背景: 项目新上线一个月,qa需要测试断电服务拉起,服务拉起成功后,发现mysql主从异常,以下是发现的问题以及解决方案 问题1: Slave_IO_Running:  No 一方面原因是因为网络通信 ...

  9. Git简易使用教程

    1.Git 安装 2.设置git登录信息 3.git操作命令 4.提交代码的过程中几个命令的顺序 5.git 学习资料. 1.Git 安装 Git 下载地址:https://git-scm.com/d ...

  10. 安装node.js、webpack、vue 和vue-cli 以及安装速度慢/不成功的解决方法

    1.安装node.js 地址:https://nodejs.org/en/  下载安装软件之后,点击下一步即可 打开dos窗口,输入cmd能快速打开,输入npm -v 和 node -v 能显示出版本 ...