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

我来叨叨一些东西。
这题肯定不能一个一个遍历求和,这样就上天了。。。
解释一下官方题解的意思。
为什么 sum(n,m)=2*sum(n-1,m)-c(n-1,m)。
因为c(n,m)=c(n-1,m)+c(n-1,m-1),至于为什么成立,不懂的百度一下组合数和杨辉三角吧。。。
sum(n,m)=c(n,0)+c(n-1,1)+c(n-1,0)+c(n-1,2)+c(n-1,1)+...+c(n-1,m)+c(n-1,m-1)//因为c(n,0)=c(n-1,0)==1
=c(n-1,0)+c(n-1,0)+c(n-1,1)+c(n-1,1)+c(n-1,2)+c(n-1,2)+...+c(n-1,m-1)+c(n-1,m-1)+c(n-1,m)
=2*sum(n-1,m)-c(n-1,m)
OK,解释完了,然后怎么做呢?
通过上面推出来的公式,我们就可以在O(1)的复杂度里由推出
。这个就可以用莫队写了。
关于莫队,具体的去看别人的博客,人家写的很好,我语文不好+懒,不想写。。。
要注意莫队的时候,while里的判断条件是当前指针对应的值与要求得的数的大小的关系,while(N<que[i].n) res=(2*res-C(N++,M)+mod)%mod;就假设,我当前的指针对应的值为sum(n-1,m),我需要得到的结果为sum(n,m),当前的N为n-1,所以我需要用公式sum(n,m)=2*sum(n-1,m)-c(n-1,m),经过这个操作,N就变成n了(因为N++)。
大体就这些,其他的就是关于组合数求解的东西了,这些代码里注释了。
代码:
//1002-6333-组合数C(n,0)到C(n,m)求和-组合数学+莫队
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int pos[maxn];
ll inv[maxn],f[maxn],ans[maxn]; struct node{
int n,m,id; bool operator <(const node &a) const{
if(pos[n]!=pos[a.n]) return n<a.n;
return m<a.m;
} }que[maxn];
/*
关于逆元
费马小定理:对于a和素数p,a^(p-1)恒等于1。
逆元的定义:对于正整数a和m,如果有a*x恒等于1,那么把这个同余方程中x的最小正整数解叫做a模m的逆元。一般用欧几里得扩展来做:ax+by=1;称a和b互为逆元
a^(p−1)=a^(p−2)∗a,所以有a^(p−2)∗a%p≡1,对比逆元的定义可得,a^(p−2)是a的逆元
所以组合数预处理------>阶乘逆元,n!%mod/[(n-m)!%mod*m!%mod],设n!%mod为A,(n-m)!%mod的逆元为B,m!%mod的逆元为C,所以组合数c(n,m)%mod=A*B%mod*C%mod,就酱~
*/
ll qpow(ll a,ll b)//快速幂a^b%mod
{
ll res=;
while(b){
if(b&) res=(res*a)%mod;
a=(a*a)%mod;
b>>=;
}
return res;
} void init()
{
f[]=;
for(int i=;i<maxn;i++)//预处理出来i!%mod,就是c(n,m)中n!%mod先预处理
f[i]=(f[i-]*i)%mod;
for(int i=;i<maxn;i++)
inv[i]=qpow(f[i],mod-);//inv中存的是逆元为a^(p-2)
} ll C(int n,int m)//求C(n,m)
{
if(n<||m<||m>n) return ;
if(m==||m==n) return ;
return f[n]*inv[n-m]%mod*inv[m]%mod;//就是A*B%mod*C%mod
} ll res=; int main()
{
init();
int T;
scanf("%d",&T);
int block=(int)sqrt(maxn);
for(int i=;i<=maxn-;i++)
pos[i]=(i-)/block;
for(int i=;i<=T;i++){
scanf("%d%d",&que[i].n,&que[i].m);
que[i].id=i;
}
sort(que+,que++T);
int N=,M=;
for(int i=;i<=T;i++){
while(N<que[i].n) res=(*res-C(N++,M)+mod)%mod;
while(N>que[i].n) res=((res+C(--N,M))*inv[])%mod;
while(M<que[i].m) res=(res+C(N,++M))%mod;
while(M>que[i].m) res=(res-C(N,M--)+mod)%mod;
ans[que[i].id]=res;
}
for(int i=;i<=T;i++){
printf("%lld\n",ans[i]);
}
return ;
}
HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))的更多相关文章
- 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 - 6333 Problem B. Harvest of Apples (莫队+组合数学)
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...
- 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 ...
- 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) 设 ...
- Problem B. Harvest of Apples HDU - 6333(莫队)
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- 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 ...
- 热身训练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( ...
随机推荐
- Cocoa & OS X & swift 4
Cocoa & OS X & swift 4 http://www.runoob.com/swift/swift-environment.html https://en.wikiped ...
- [剑指Offer] 28.数组中出现次数超过一半的数字
[思路]将每个数字都存入map中作为key值,将它们出现的次数作为value值,当value超过一半时则返回其key值. class Solution { public: int MoreThanHa ...
- 【Python】Python中子类怎样调用父类方法
python中类的初始化方法是__init__(),因此父类子类的初始化方法都是这个,如果子类不实现这个函数,初始化时调用父类的初始化函数,如果子类实现这个函数,就覆盖了父类的这个函数,既然继承父类, ...
- DataBase -- Employees Earning More Than Their Managers My Submissions Question
Question: The Employee table holds all employees including their managers. Every employee has an Id, ...
- 【题解】HNOI2018寻宝游戏
太厉害啦……感觉看到了正解之后整个人都惊呆了一样.真的很强%%% 首先要注意到一个性质.位运算列与列之间是不会相互影响的,那么我们先观察使一列满足条件的操作序列需要满足什么条件.&0时,不论之 ...
- MFC Button控件自绘
文章参考地址: http://blog.csdn.net/yue7603835/article/details/6649458 VC下的界面着实难看 有时候我们不得不自己进行控件的绘制 以前 ...
- BZOJ4569 [SCOI2016]萌萌哒 【并查集 + 倍增】
题目链接 BZOJ4569 题解 倍增的思想很棒 题目实际上就是每次让我们合并两个区间对应位置的数,最后的答案\(ans = 9 \times 10^{tot - 1}\),\(tot\)是联通块数, ...
- 迅雷Bolt的ClipSubBindBitmap函数特别说明
因为在工作中基于迅雷Bolt开发的是IM产品,需要实现自定义用户头像的功能. 但Bolt中对图像的默认拉伸锯齿效果非常明显,所以自己实现了图像拉伸函数,代码已共享,具体可查看:<迅雷Bolt图像 ...
- 创建 React 项目
依次输入命令: npm install -g create-react-app create-react-app react16 cd my-app npm start 在浏览器中输入 local:3 ...
- Linux下文件解压命令
1.压缩命令: 命令格式:tar -zcvf 压缩文件名.tar.gz 被压缩文件名 可先切换到当前目录下.压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式:tar -zxvf 压缩 ...