Problem B. Harvest of Apples HDU - 6333(莫队)

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

 

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

 

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

 

Sample Input
2 5 2 1000 500
 

 

Sample Output
16 924129523
 

 

Source
2018 Multi-University Training Contest 4
 

 解析:

   Problem B. Harvest of Apples HDU - 6333(莫队)_第1张图片

  

#include 
#include 
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
#include 
#include 
#include 
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define LL long long
#define ULL unsigned long long
#define Pair pair
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + 100, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
const int MOD = 1e9+7;
LL n, m, ans;
LL up[maxn], down[maxn], pos[maxn], inc[maxn], inv[maxn];

struct node
{
    LL l, r;
    int id;
}Node[maxn];

bool cmp(node a, node b)
{
    return pos[a.l] == pos[b.l] ? (a.r < b.r) : (a.l < b.l);
}

LL qp(LL a, LL b)
{
    LL res = 1;
    while(b)
    {
        if(b & 1) res  = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

void init()
{
    up[0] = 1;
    down[0] = 1;
    for(int i=1; i)
    {
        up[i] = up[i-1] * i % MOD;
        down[i] = qp(up[i], MOD - 2) % MOD;
    }
}

LL C(LL n, LL m)
{
    if(n < m) return 0;
    return up[n] * down[n-m] % MOD * down[m] % MOD;
}

int main()
{
    init();
    int block = sqrt(100000);
    for(int i=1; i<=100000; i++)
        pos[i] = (i-1)/block + 1;
    int T;
    rd(T);
    for(int i=1; i<=T; i++)
    {
        rlld(Node[i].r), rlld(Node[i].l);
        Node[i].id = i;
    }
    sort(Node + 1, Node + T + 1, cmp);
    ans = 2;
    int tmp = qp(2, MOD - 2);
    for(int i=1, l=1, r=1; i<=T; i++)
    {
        for(; r < Node[i].r; r++)
            ans = (2 * ans - C(r, l) + MOD) % MOD;
        for(; r > Node[i].r; r--)
            ans = (ans + C(r-1, l)) * tmp % MOD;
        for(; l < Node[i].l; l++)
            ans = (ans + C(r, l+1)) % MOD;
        for(; l > Node[i].l; l--)
            ans = (ans - C(r, l) + MOD) % MOD;
        if(Node[i].l == Node[i].r)
        {
            inc[Node[i].id] = 1;
        }

        inc[Node[i].id] = ans;
    }
    for(int i=1; i<=T; i++)
        printf("%lld\n", inc[i]);


    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9695721.html

你可能感兴趣的:(Problem B. Harvest of Apples HDU - 6333(莫队))