hdu6333 Problem B. Harvest of Apples

题目传送门

题意:

求(0,n)~(m,n)组合数之和

题解:

C(n,m)=C(n-1,m-1)+C(n-1,m)   
设 S(n,m)=C(n,0)+C(n,1)+C(n,2)+。。。+C(n,m)
然后将S(n,m) 通过 第一个公式 拆项
最后化简 变为 S(n,m)=2*S(n-1,m)-C(n-1,m);
即:
所以可以离线用莫队算法
参考博客:链接1链接2

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int mod=1e9+;
const int N=1e5+; /********组合数模板*********/
ll pow_mod(ll a, ll b) {
ll res = 1ll; a %= mod;
while(b){
if(b & ) res = res * a % mod;
a = a * a % mod;
b >>= ;
} return res;
}
ll inv(ll a) { return pow_mod(a, mod-); }
ll F[N], Finv[N];//F是阶乘,Finv是逆元的阶乘
void init() {
F[] = Finv[] = 1ll;
for(int i = ; i < N; i ++){
F[i] = F[i-] * 1ll * (ll)i % mod;
Finv[i] = Finv[i-] * 1LL * inv(i) % mod;
}
} // O(n)预处理
ll C(ll n, ll m) {
if(m < || m > n) return ;
return F[n] * 1LL * Finv[n - m] % mod * Finv[m] % mod;
} // O(1)获得组合数C(n,m)
/**************************/ int block[N];
ll res[N];
struct mo
{
int n,m;
int id,block;
bool operator < (const mo &p) const{
if(block==p.block) return n<p.n;
else return block<p.block;
}
}s[N];
int main()
{
int T;
scanf("%d",&T);
int len=sqrt(N+0.5);
for(int i=;i<T;i++)
{
scanf("%d %d",&s[i].n,&s[i].m);
s[i].block=s[i].m/len;s[i].id=i;
}
sort(s,s+T);
ll ans=;
init();
int L=,R=;
for(int i=;i<T;i++)
{
int l=s[i].n,r=s[i].m;
while(L>l) ans=((ans+C(L-1LL,R))%mod*Finv[])%mod, L--;
while(L<l) ans=(*ans%mod-C(L,R)+mod)%mod, L++;
while(R<r) ans=(ans+C(L,R+))%mod, R++;
while(R>r) ans=(ans-C(L,R)+mod)%mod, R--;
res[s[i].id]=ans;
}
for(int i=;i<T;i++)
printf("%lld\n",res[i]);
return ;
}

hdu6333 Problem B. Harvest of Apples(组合数+莫队)的更多相关文章

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

  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. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  4. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

  5. HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...

  6. 【HDOJ6333】Harvest of Apples(莫队)

    题意: 给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模 T<=1e5 1<=n,m<=1e5 思路: 注意要先变n再变m,否则会因 ...

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

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. 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. 在eclipse里搜索maven项目需要的dependency

    eclipse直接就可以通过下载同步仓库索引,直接关键字查询需要的dependency. 前提是你已经在你的eclipse上配好了maven正确的环境. 1. 设置在开启eclipse时下载同步仓库索 ...

  2. Redis 复制原理及特性

    摘要 早期的RDBMS被设计为运行在单个CPU之上,读写操作都由经单个数据库实例完成,复制技术使得数据库的读写操作可以分散在运行于不同CPU之上的独立服务器上,Redis作为一个开源的.优秀的key- ...

  3. node.js从入门到放弃《模块》

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很 ...

  4. 02.action--新增精灵知识点

    import cocos from cocos.actions import * class HelloWorld(cocos.layer.ColorLayer): # ColorLayer子类化为具 ...

  5. JavaWeb(七):EL表达式、自定义标签和JSTL

    一.EL表达式 语法 el.jsp <%@page import="java.util.Date"%> <%@page import="com.atgu ...

  6. centos启动提示unexpected inconsistency RUN fsck MANUALLY

    今天一台虚拟机背后的物理机故障了,主机迁移后变成了 read only filesystem.上面部署了很多长连接服务,没有关掉就直接reboot,报错: unexpected inconsisten ...

  7. TFRecords文件的生成和读取(1)

    参考:https://blog.csdn.net/u012222949/article/details/72875281 参考:https://blog.csdn.net/chengshuhao199 ...

  8. Jira插件安装

    以下操作需要反编译 1.反编译的jar包 1)E:\JIRA安装路径\atlassian-jira\WEB-INF\atlassian-bundled-plugins\atlassian-univer ...

  9. 【説明する】STL

    作为C++标准不可缺少的一部分,STL应该是渗透在C++程序的角角落落里的. STL不是实验室里的宠儿,也不是程序员桌上的摆设,她的激动人心并非昙花一现. 所以今天要整理的东西就是STL!(orz 杨 ...

  10. Queue1循环队列

    循环队列 1 #include<iostream> using namespace std; //#define maxSize 20 template <class T> c ...