题意:

给定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. SIRI课程表

    wen 周一@0@{今天没课哦}周一 周二今天共一节课 第2节,可编程控制器应用,11号教学楼1 0 3房间 周二 周三今天共三节课 第二节,过程控制系统,2号楼2 1 0房间,第三节机械制造技术11 ...

  2. spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...

  3. SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题

    1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...

  4. QTableWidget表头样式

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...

  5. jmeter+ant+jenkins

    前提:需要先配置下面两个环境,严格按照本人的配置去配,要不然后面你会看不懂 (1)ant+jmeter集成:http://blog.csdn.net/qq_23101033/article/detai ...

  6. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  7. liunx 修改IP地址

    1.安装centos系统,这里就不详细说明了. 2.进入到 vi /etc/sysconfig/network-scripts/ifcfg-eth0      后面的名称有些可能不同 其中,有些可能没 ...

  8. php bz2扩展安装

    php bz2扩展安装 2017年09月22日 14:14:36 Cookie_1030 阅读数:1781   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...

  9. vue点击时动态改变样式 ------- 最简单的方法

    vue点击时动态改变样式 template中 <li :class="{ active:index==isActive }" @click="changeValue ...

  10. 牛客OI赛制测试赛2 C 数组下标

    链接:https://www.nowcoder.com/acm/contest/185/C来源:牛客网 题目描述 给出一个数列 A,求出一个数列B. 其中Bi   表示 数列A中 Ai 右边第一个比 ...