Harvest of Apples

问题 B: Harvest of Apples

时间限制: 1 Sec  内存限制: 128 MB
提交: 18  解决: 11
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

输入

The first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).

输出

For each test case, print an integer representing the number of ways modulo 109+7.

样例输入

2
5 2
1000 500

样例输出

16
924129523

莫队(分块),组合数学。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int mod=1e9+7;
ll fac[maxn],inv[maxn],ans[maxn];
ll rev2,res;
int pos[maxn];
ll qpow(ll b,int n)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=res*b%mod;
        b=b*b%mod;
        n>>=1;
    }
    return res;
}
ll Comb(int n,int k)
{
    return fac[n]*inv[k]%mod*inv[n-k]%mod;
}
void pre()
{
    rev2=qpow(2,mod-2);
    fac[0]=fac[1]=1;
    for(int i=2; i<maxn; ++i) fac[i]=i*fac[i-1]%mod;
    inv[maxn-1]=qpow(fac[maxn-1],mod-2);
    for(int i=maxn-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
struct Query
{
    int L,R,id;
    bool operator <(const Query& p) const
    {
        if(pos[L]==pos[p.L]) return R<p.R;
        return L<p.L;
    }
} Q[maxn];
inline void addN(int posL,int posR)
{
    res=(2*res%mod-Comb(posL-1,posR)+mod)%mod;
}
inline void addM(int posL,int posR)
{
    res=(res+Comb(posL,posR))%mod;
}
inline void delN(int posL,int posR)
{
    res=(res+Comb(posL-1,posR))%mod*rev2%mod;
}
inline void delM(int posL,int posR)
{
    res=(res-Comb(posL,posR)+mod)%mod;
}
int main()
{
    int T,curL,curR;
    int block=(int)sqrt(1.0*maxn);
    pre();
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
    {
        scanf("%d %d",&Q[i].L,&Q[i].R);
        pos[i]=i/block;
        Q[i].id=i;
    }
    sort(Q+1,Q+T+1);
    res=2;
    curL=1,curR=1;
    for(int i=1;i<=T;++i)
    {
        while(curL<Q[i].L) addN(++curL,curR);
        while(curR<Q[i].R) addM(curL,++curR);
        while(curL>Q[i].L) delN(curL--,curR);
        while(curR>Q[i].R) delM(curL,curR--);
        ans[Q[i].id] = res;
    }
    for(int i=1;i<=T;++i) printf("%lld\n",ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/lglh/p/9406264.html

时间: 2024-10-03 08:19:33

Harvest of Apples的相关文章

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

Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1600 Accepted Submission(s): 604 Problem Description There are n apples on a tree, numbered from 1 to n. Count the nu

HDU 6311 Harvest of Apples (组合数,莫队)

场上怎么都想不出来,看了标程想自闭... #include <algorithm> #include <iostream> #include <cstdio> #include <vector> #include <cmath> using namespace std; #define N 100005 #define mod 1000000007 struct query{ int n,k,i; }Q[N]; bool cmp(const qu

Problem B. Harvest of Apples

PS:没看出这个可以离线...官方题解 标程貌似不是常规莫队的写法,同一个块内,没有对 r 排序,优先处理每个块的答案.学习到了阶乘逆元的递推公式. inv[mx] = powi(fac[mx], mod - 2); for(int i = mx - 1; ~i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; 避免加法溢出 while(in < lst[i][j].n) val = (0ll + val + val + mod - C(in++,

HDU-6333 Problem B. Harvest of Apples 莫队

HDU-6333 题意:有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路:这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的方法总数,显然是C(n,0),C(n,1)……C(n,m)的和. 显然S(n,m+1) = S(n, m) + C(n,m+1); 还有一个等式就不那么明显了,S(n+1,m) = 2 * S(n,m) - C(n,m); 我也是在王神犇的指导下明白的. 既然知道了一组(n,m)是可以在很快的时间下转移

HDU 6333 莫队+组合数

Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2397    Accepted Submission(s): 934 Problem Description There are n apples on a tree, numbered from 1 to n.Count th

2018 Multi-University Training Contest 4 Solution

A - Problem A. Integers Exhibition 留坑. B - Problem B. Harvest of Apples 题意:计算$\sum_{i = 0}^{i = m}C(n, i)$ 思路:由$sum_{i = 0}^{i = m}C(n,i)$可以得到$sum_{i = 0}^{i = m + 1}C(n,i)$以及$sum_{i = 0}^{i = m}C(n + 1,i)$然后用莫对算法求解 1 #include<bits/stdc++.h> 2 3 usi

莫队专题

https://blog.csdn.net/qq_41552508/article/details/100556943附上学习连接 以防万一还是搬出来吧 一.适用问题 莫队算法是一种离线算法,用分块去优化暴力,不包含修改的话,复杂度为 O(nn−−√+mn−−√) O(n\sqrt n+m\sqrt n)O(n n ? +m n ? ),n nn 为序列长度,m mm 为操作总数. 二.算法实现 莫队本质上就是用分块去优化暴力的离线算法,将总复杂度降到 O(nn−−√) O(n\sqrt n)O

Pick apples 第三届acm省赛

Description Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each ki

HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road. The ith tree is planted at position xi,