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. C# Windows Services 启动和结束其它进程

    将exe所在的绝对路径和进程名配置到配置文件中 <add key="FilePath" value="D:\ABC\ABCD.Console.exe"/& ...

  2. 手机端 css 样式重置

    @charset "utf-8"; body, div, ul, li, ol, h1, h2, h3, h4, h5, h6, input, textarea, select, ...

  3. 常见3种Git服务器的构建

    学习Git不同的服务器形式,具体如下: - 创建SSH协议服务器 - 创建Git协议服务器 - 创建HTTP协议服务器 方案: Git支持很多服务器协议形式,不同协议的Git服务器,客户端就可以使用不 ...

  4. ps:图像尺寸

    在课程#01中我们知道了显示器上的图像是由许多点构成的,这些点称为像素,意思就是“构成图像的元素”.但是要明白一点:像素作为图像的一种尺寸,只存在于电脑中,如同RGB色彩模式一样只存在于电脑中.像素是 ...

  5. php session之redis存储

    前提:redis已安装好. php代码: <?php ini_set("session.save_handler", "redis"); ini_set( ...

  6. CSS3弹性盒模型flexbox布局

    属性介绍 display: flex | inline-flex; (适用于父类容器元素上) 定义一个flex容器,内联或者根据指定的值,来作用于下面的子类容器.· box:将对象作为弹性伸缩盒显示. ...

  7. AIX系统软件安装问题

    一.安装软件时一定要cd到介质目录中 二.选择accept new licence 三.更新系统时避免使用updata_all,要手动选择出要更新的软件 四.oracle11G的rac还要用到open ...

  8. SSM三大框架整合梳理

    整合步骤 0.搭建动态web项目 1.需要的jar包 spring(包括springmvc) mybatis相关jar包 mybatis与spring的整合包(个人建议尽量使用高版本的,避免出现一些奇 ...

  9. IDEA git 合并多个commit

    当前三个commit,demo1,demo2,demo3 选择demo1右键 选择action 跟着指示操作,最后合并 时间线: Log 框时间线:是从上到下,越来越早. 弹出框时间线:是从上到下,越 ...

  10. 关于python3 使用pycharm+unittest+html+HTMLTestRunner 测试用例运行正常,但却不能生成测试报告的解决方法

    关于python3 使用pycharm+unittest+html+HTMLTestRunner 测试用例运行正常,但却不能生成测试报告的解决方法 这个问题我也遇到过,以下是解决办法   该方法适用于 ...