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. Linux架构之NFS共享存储1

    第35章 NFS共享存储 35.1 NFS基本概述 NFS是Network File System的缩写及网络文件系统.NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录. 常见的文件 ...

  2. CentOS7安装mysql8.0编译报错集合

    以下都是我安装mysql8.0遇到的一些报错和解决方法 1.does not appear to contain CMakeLists.txt. 原因:mysql下载的源码包不对 解决方法:下载正确的 ...

  3. LightOJ 1289 LCM from 1 to n(位图标记+素数筛

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  4. git clone的低级错误

    犯了一个低级错误: server ip: 192.168.40.41 有一个git账户 所有的git仓库都在/home/git仓库下 比如/home/git/u-boot-2018.07-fmxx.g ...

  5. tf.matmul / tf.multiply

    import tensorflow as tfimport numpy as np 1.tf.placeholder placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时 ...

  6. [web 安全] 源码泄露

    web 源码泄露 1..hg 源码泄露 http://www.example.com/.hg/ 2..git 源码泄露 http://www.example.com/.git/config 3..ds ...

  7. Test 7.12 T2

    题目描述 ​ 有一张 n 个点 m 条边的无向图,其中有 s 个点上有加油站.有 Q 次询问(a,b,c), 问能否开一辆油箱容积为 c 的车从 a 走到 b. 输入格式 ​ 第一行三个整数 n,s, ...

  8. Centos添加硬盘分区

    1. 查看硬盘信息 fdish -l 此处/dev/sdb为新添加硬盘 2. 格式化为ext4硬盘格式 mkfs.ext4 /dev/sdb 亦可使用其他格式 硬盘空间大于2T时,MBR分区无法识别更 ...

  9. 一个能极大提高生产率的Chrome新建标签页扩展

    我是一个对开发生产率有着BT需求的程序员,总是追求将自己的单位时间生产率最大化. 通过分析,我发现自己一天会反反复复使用Chrome的新建标签,然后访问常用的网站.因此,我期望新建一个默认的Chrom ...

  10. 十二 windows临界区,其他各种mutex

    一.windows临界区 类似于互斥量 == 临界区. 二.多次进入临界区 进入临界区(加锁): 离开临界区(解锁): 同一个线程中windows中相同临界区变量代表的临界区进入(entercirti ...