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( ...
随机推荐
- 【python】Python 字典(Dictionary)操作详解
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: dict = {'} ...
- ElasticSearch1.7.1拼音插件elasticsearch-analysis-pinyin-1.3.3使用介绍
ElasticSearch拼音插件elasticsearch-analysis-pinyin使用介绍 https://my.oschina.net/xiaohui249/blog/214505 摘要: ...
- 【bzoj1263】[SCOI2006]整数划分 高精度
题目描述 从文件中读入一个正整数n(10≤n≤31000).要求将n写成若干个正整数之和,并且使这些正整数的乘积最大. 例如,n=13,则当n表示为4+3+3+3(或2+2+3+3+3)时,乘积=10 ...
- Codeforces Round #553 F Sonya and Informatics
题目 题目大意 给定一个长为 $n$($2 \le n \le 100$)的01串 $S$ .对 $S$ 进行 $k$($1 \le k \le 10^9$)次操作:等概率地选取两个下标 $i, j$ ...
- 移动开发:美团外卖Android Lint代码检查实践
概述 Lint是Google提供的Android静态代码检查工具,可以扫描并发现代码中潜在的问题,提醒开发人员及早修正,提高代码质量.除了Android原生提供的几百个Lint规则,还可以开发自定义L ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- 如何把阿里云的服务器配置为mac的共享文件夹(亲测有效)
写在开头的就是,我只能百分之九十确定这个是真的有效....毕竟试了太多的方法,最后莫名其妙的就好了.. - -# 基础的步骤就不说了,网上一搜一大把,大家可能follow了所有的步骤以后发现还是连接不 ...
- Palindrome [Manecher]
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12214 Accepted: 4583 Descript ...
- cloudera manager 5.3完整卸载脚本
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/p ...
- Spring学习-- SpEL表达式
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...