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( ...
随机推荐
- Xcode & swift
swift-apps swift 2018 apps Xcode Swift Playground https://developer.apple.com/download/ https://deve ...
- [bzoj1056] [HAOI2008]排名系统
Description 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务 ...
- Nginx的火速蔓延与其并发性处理优势
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器.Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器.Ngi ...
- POJ3259:Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 68097 Accepted: 25374 题目链接: ...
- angular js module 的理解
module其实就是一个容器,里面可以装controller,service,directive,filter等, 官网的解释是:Module :A container for the differe ...
- Prepare and Deploy Windows Server 2016 Active Directory Federation Services
https://docs.microsoft.com/en-us/windows/security/identity-protection/hello-for-business/hello-key-t ...
- dbcp重连问题排查
转载自:http://lc87624.iteye.com/blog/1734089 使用数据库连接池时,免不了会遇到断网.数据库挂掉等异常状况,当网络或数据库恢复时,若无法恢复连接池中的连接,那必然会 ...
- 修改firefox默认下载路径
菜单栏---编辑---首选项--在常规页就可以看到下载设置了
- (转)如何用python抓取网页并提取数据
最近一直在学这部分,今日发现一篇好文,虽然不详细,但是轮廓是出来了: 来自crifan:http://www.crifan.com/crawl_website_html_and_extract_inf ...
- Ubuntu pppoe 拨号上网
-------------蓝色是终端里面的连接方式,可以不看--------------------- ADSL上网,Ubuntu下是可以的,虽然以前没用过拨号上网,不过查了查也不是很麻烦. 打开终端 ...