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. win10去除快捷方式小箭头

    切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...

  2. java课堂_动手动脑4

    1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 答:在Java中,内容相同的字串常量(“Hello”)只保存一份以节约内存,所以s0 ...

  3. JWT详解

    目录 1.前言 2.JWT的数据结构 2.1 Header 2.2 Payload 2.3 Signature 2.4 Base64URL 3. JWT的实现   1.前言 定义:JSON Web T ...

  4. hadoop安装解决之道

    # 壹.故障现象 ```xml Microsoft Windows [版本 10.0.18362.239] (c) 2019 Microsoft Corporation.保留所有权利. C:\User ...

  5. JAVA基础知识(三):input.nextLine() 和input.next()

    next()方法在读取内容时,会过滤掉有效字符前面的无效字符,对输入有效字符之前遇到的空格键.Tab键或Enter键等结束符,next()方法会自动将其过滤掉:只有在读取到有效字符之后,next()方 ...

  6. 转载 | Sublime Text3 安装以及初次配置

    本文引自:http://blog.csdn.net/u011272513/article/details/52088800 工具:官网下载:Sublime Text3 安装:直接运行安装.http:/ ...

  7. GBK和UTF-8的区别

    我们这里将以最简单最容易理解的方式来描述GBK和UTF8的区别,以及它们分别是什么.   GBK编码:是指中国的中文字符,其它它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符 ...

  8. hdu1241 油田计数

    具体思路:求联通块,在"@“的周围进行dfs,使用8个方向向量来代表搜索的方向 贴一下我的主要代码段: int dir[8][2]={{1,1},{-1,-1},{1,-1},{-1,1}, ...

  9. FLV协议5分钟入门浅析

    FLV协议简介 FLV(Flash Video)是一种流媒体格式,因其体积小.协议相对简单,很快便流行开来,并得到广泛的支持. 常见的HTTP-FLV直播协议,就是使用HTTP流式传输通过FLV封装的 ...

  10. PHP版本的区别与用法详解

    在我们安装PHP模块时,有时需要注意PHP编译的版本,下面讲解下PHP中VC6.VC9.TS.NTS版本的区别与用法详解,介绍php的两种执行方式. 1. VC6与VC9的区别:VC6版本是使用Vis ...