题意:

给定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. 洛谷P2742 【模板】二维凸包

    题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...

  2. 浅析Statement和PreparedStatement的区别

    当我们使用java程序来操作sql server时会使用到Statement和PreparedStatement,俩者都可以用于把sql语句从java程序中发送到指定数据库,并执行sql语句.那么如何 ...

  3. cordova安装方法

    安装cordova之前需要先安装node.js和npm 然后在命令行运行:npm install -g cordova即可安装到最新版本的cordova 如果要安装指定版本,命令为:npm insta ...

  4. 杨辉三角python的最佳实现方式,牛的不能再牛了

    def triangles(): N = [1] while True: yield N N.append(0) N = [N[i-1] + N[i] for i in range(len(N))] ...

  5. ios---setContentOffset

    UIView * farmeView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,  self. ...

  6. Java集合类工具CollectionUtils的操作方法

    集合判断: 例1: 判断集合是否为空:CollectionUtils.isEmpty(null): trueCollectionUtils.isEmpty(new ArrayList()): true ...

  7. scroll offset & client总结

    oEvent.clientX 是指鼠标到可视区左边框的距离. oEvent.clientY 是指鼠标到可视区上边框的距离. clientWidth  是指可视区的宽度. clientHeight  是 ...

  8. windows安装tensorflow的一个教训

    今天没什么课,然后就准备安装tensorflow. 看了一下教程,就去做了. 然后就犯了错误.网上的教程还是有一些差异的,而我又比较大意,没有很注意到CUDA,cudnn的版本要求,也过于高估自己cp ...

  9. mysql数据库比较,各数据库不同之处

    和mysql数据库比较,各数据库不同之处: Oracle数据库:字段类型不同 postgresql数据库:show tables不同; SQL语句需要前面加上 模式名 Mongodb数据库:文档存储, ...

  10. faster rcnn训练过程讲解

    http://blog.csdn.net/u014696921/article/details/60321425