题意:

给定T组询问,每组有两个数字n和m,求sigma i=0..m c(n,i) 答案对1e9+7取模

T<=1e5

1<=n,m<=1e5

思路:

注意要先变n再变m,否则会因n太小有些组合数会丢失

关键点在于n的转移,m的转移谁都会

预处理数组越界要小心

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define MOD 1000000007
#define N 110000 struct data{int x,y,id;}a[N];
ll ans[N+],fac[N+],inv[N+],inv2;
int pos[N+],m; bool cmp(data a,data b)
{
if(pos[a.x]==pos[b.x]) return a.y<b.y;
return a.x<b.x;
} int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} ll c(int x,int y)
{
ll t=fac[x]*inv[y]%MOD*inv[x-y]%MOD;
return t;
} void solve()
{
ll sum=;
for(int i=;i<=a[].y;i++) sum=(sum+c(a[].x,i))%MOD;
int nowx=a[].x,nowy=a[].y;
ans[a[].id]=sum;
for(int i=;i<=m;i++)
{
while(nowx>a[i].x)
{
sum=(sum+c(nowx-,nowy))%MOD;
sum=sum*inv2%MOD;
nowx--;
}
while(nowx<a[i].x)
{
sum=(sum*-c(nowx,nowy)+MOD)%MOD;
nowx++;
}
while(nowy>a[i].y)
{
sum=(sum-c(nowx,nowy)+MOD)%MOD;
nowy--;
}
while(nowy<a[i].y)
{
sum=(sum+c(nowx,nowy+)+MOD)%MOD;
nowy++;
}
ans[a[i].id]=sum;
}
} void init()
{
fac[]=;
for(int i=;i<=N;i++) fac[i]=fac[i-]*i%MOD;
inv[]=inv[]=;
for(int i=;i<=N;i++) inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
inv2=inv[];
for(int i=;i<=N;i++) inv[i]=inv[i-]*inv[i]%MOD;
int block=int(sqrt(N));
for(int i=;i<=N;i++) pos[i]=(i-)/block+;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
}
init();
sort(a+,a+m+,cmp);
solve();
for(int i=;i<=m;i++) printf("%lld\n",ans[i]);
return ;
}

【HDOJ6333】Harvest of Apples(莫队)的更多相关文章

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

  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. HDU-6333 Problem B. Harvest of Apples 莫队

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

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

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

  6. HDU - 6333:Harvest of Apples (组合数前缀和&莫队)

    There are n n apples on a tree, numbered from 1 1 to n n . Count the number of ways to pick at most ...

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

  8. hdu多校第4场 B Harvest of Apples(莫队)

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

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

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

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

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

随机推荐

  1. Java并发——ThreadPoolExecutor线程池解析及Executor创建线程常见四种方式

    前言: 在刚学Java并发的时候基本上第一个demo都会写new Thread来创建线程.但是随着学的深入之后发现基本上都是使用线程池来直接获取线程.那么为什么会有这样的情况发生呢? new Thre ...

  2. flutter基础

    1.flutter安装 1.参考官网安装sdk https://flutter.io/get-started/install 安卓和IOS需要分别配置对应的开发环境,安卓建议使用as开发,安装Flut ...

  3. Oracle的数据伪列(ROWNUM)

    作者:Vashon 时间:20150414 数据库:Oracle11g 数据伪列(ROWNUM) *范例:查询前5条记录:select rownum,empno,job,hiredate,sal fr ...

  4. Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)

    1. 我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed& ...

  5. Chrome浏览器商店安装的插件保存到本地

    Chrome自67版本开始,不能从第三方下载插件拖动安装,要么就是以前的方法安装修改为zip格式,会有报错(报错如下图),强迫症肯定忍不了报错的:按照网上说法,是第三方插件的压缩算法和Chrome商店 ...

  6. 数论基础之组合数&计数问题

    一.组合数:问题引入:现在有 n 个球,取其中的 k 个球,问一共有多少种方式?答案: 公式直观解释:我们考虑有顺序地取出 k 个球:第一次有 n 种选择,第二次有 n-1 种选择,...,第 k 次 ...

  7. 使用Spring IoC进行Bean装配

    Spring概述 Spring的设计严格遵从的OCP(开闭原则),保证对修改的关闭,也就是外部无法改变spring内部的运行流程:提供灵活的扩展接口,也就是可以通过extends,implements ...

  8. Shading-jdbc源码分析-sql词法解析

    前言 前有芋艿大佬已经发过相关分析的文章,自己觉的源码总归要看一下,然后看了就要记录下来(记性很差...),所以就有了这篇文章(以后还要继续更

  9. kvm安装图终端界面及形界面安装系统

    1.图形界面安装: qemu-img create -f qcow2 /kvm/os/vm-01.qcow2 16G mkdir -p /kvm/iso cd /kvm/iso 上传事先下载好的镜像文 ...

  10. fit in gnuplot

    Table of Contents 1. fit-gnuplot 1 fit-gnuplot syntax >> fit [xrange][yrange] function 'datafi ...