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))的更多相关文章

  1. 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 ...

  2. 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)).有公 ...

  3. 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 ...

  4. 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)    设 ...

  5. 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 ...

  6. 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数值范围太 ...

  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 莫队求组合数前缀和

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

  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. ELK + Kafka + Filebeat

    ELK + Kafka + Filebeat学习 https://blog.csdn.net/qq_21383435/article/details/79463832 https://blog.csd ...

  2. WebSocket API使用篇检测浏览器是否支持WebSocket(4)

    WebSocket API是下一代客户端-服务器的异步通信方法.前面有三篇文章已经对WebSocket有了一些介绍,这里我总结了一下.我在使用WebSockets API过程中遇到的问题. 1.检测浏 ...

  3. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...

  4. 【C++ troubleshooting】A case about decltype

    template <typename iter_t> bool next_permutation(iter_t beg, iter_t end) { // if (beg == end | ...

  5. BZOJ2118 墨墨的等式 【最短路】

    题目链接 BZOJ2118 题解 orz竟然是最短路 我们去\(0\)后取出最小的\(a[i]\),记为\(p\),然后考虑模\(p\)下的\(B\) 一个数\(i\)能被凑出,那么\(i + p\) ...

  6. 洛谷 P2444 [POI2000]病毒 解题报告

    P2444 [POI2000]病毒 题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已 ...

  7. eclipse安装反编译插件jadclipse

    下载jadClipse地址: 目的:将一些封装的jar或者sdk可以查看源代码 链接: http://pan.baidu.com/s/1kTN4TPd  提取码: 3fvd 将net.sf.jadcl ...

  8. mysql_存储过程和函数

    存储过程和函数 1.什么是存储过程和函数 存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句集合,调用存储过程和函数可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对 ...

  9. i=i+1与i+=1的区别及效率(Java)

    原博客地址 在做个java优化的PPT时,看到了i=i+1与i+=1的区别,在这之前还真没想到那么细. 1.x=x+1,x+=1及x++的效率哪个最高?为什么? x=x+1最低,因为它的执行如下. ( ...

  10. web上传组件

    uploadify  jquery插件. common-fileipload; common-io ;jar