杭电多校 Harvest of Apples 莫队

问题 B: Harvest of Apples

时间限制: 1 Sec  内存限制: 128 MB
提交: 78  解决: 35
[提交] [状态] [讨论版] [命题人: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

 

[提交][状态]

官方题解

重点就是划出来的公式,然后套莫队即可

代码:

#include <bits/stdc++.h>
using namespace std;
const  int maxn=1e5+100;
#define INF 0x3f3f3f;
const int mod=1e9+7;
typedef long long ll;
struct node
{
    ll n,m;
    int p;
}s[maxn];
ll block;
ll fac[maxn],inv[maxn];
ll ans[maxn];
ll noww;
ll qpow(ll x,ll y){
    ll ret = 1;
    while(y){
        if(y&1) ret = ret * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ret;
}
void init(){
    fac[0] = 1;
    for(int i=1; i<maxn; i++) fac[i] = fac[i-1] * i % 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;
}
ll C(ll x,ll y)
{
    return fac[x] * inv[y] %mod * inv[x-y] % mod;
}
bool cmp(node x,node y)
{
    if(x.n / block == y.n / block)  return x.m < y.m;
    return x.n / block < y.n / block;
}
int main()
{
    int t;
    scanf("%d",&t);
    block = sqrt(maxn);
    init();
    for(int i=0; i<t; i++){
        scanf("%lld%lld",&s[i].n,&s[i].m);
        s[i].p = i;
    }
    sort(s,s + t,cmp);
    ll l = 1,r = 1;
    noww = 2;
    for(int i=0; i<t; i++){
        while(l < s[i].n){
            l++;
            noww = (2 * noww % mod - C(l-1,r)  + mod) % mod;
        }
        while(l > s[i].n){
            noww = (noww + C(l-1,r)) % mod * inv[2] % mod;
            l--;
        }
        while(r < s[i].m){
            r++;
            noww = (noww + C(l,r)) % mod;
        }
        while(r > s[i].m){
            noww = (noww + mod - C(l,r)) % mod;
            r--;
        }
        ans[s[i].p] = noww;
    }
    for(int i=0; i<t; i++){
        printf("%lld\n",ans[i]);
    }
    return 0;
}

刚刚学了莫队,赶紧把原来两个莫队给补了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值