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 number of ways to pick at most m apples.

Input
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).

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

Recommend
chendu   |   We have carefully selected several similar problems for you:  6343 6342 6341 6340 6339 

求C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

设S(n,m)=C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

第一个式子易得,第二个式子:杨辉三角的 n,m=(n-1,m)+(n-1,m-1)

那么就是这一行等于上一行的都用了2次,只有第最后一个用了一次

所以减去c(n-1,m)

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
#define ll long long
const int maxn=1e5+7;
ll jiecheng[maxn],inv[maxn];
ll ans[maxn];
int block;
ll qsm(ll a,ll b)
{
    ll ans=1;
    while(b){
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
void init()
{
    jiecheng[1] = 1;
    for(int i = 2; i < maxn; i++)
        jiecheng[i] = jiecheng[i-1] * i % mod;
    for(int i = 1; i < maxn; i++)
        inv[i] = qsm(jiecheng[i], mod-2);
}
struct node{
    int l,r;
    int i;
}modui[maxn];
bool cmp(node a,node b)
{
    if(a.l/block==b.l/block)
        return a.r<b.r;
    return a.l<b.l;
}
ll C(ll n,ll m)
{

    if(m == 0 || m == n) return 1;
    ll ans=1;
    ans=(jiecheng[n]*inv[m])%mod*inv[n-m];
    ans=ans%mod;
    return ans;
}
int main()
{
    init();
    block = sqrt(maxn);
    int t;
    scanf("%d",&t);
    for(int i=0;i<t;i++)
    {
        scanf("%d%d",&modui[i].l,&modui[i].r);
        modui[i].i=i;
    }
    sort(modui,modui+t,cmp);
    int l=1,r=0;
    int sum=1;
    for(int i = 0; i < t; i++)
    {
        while(l < modui[i].l) sum = (2 * sum - C(l++, r) + mod) % mod;
        while(l > modui[i].l) sum = ((sum + C(--l, r))*inv[2]) % mod;
        while(r < modui[i].r) sum = (sum + C(l, ++r)) % mod;
        while(r > modui[i].r) sum = (sum - C(l, r--) + mod) % mod;
        ans[modui[i].i] = sum;
    }
    for(int i=0;i<t;i++)
    {
        printf("%lld\n",ans[i]);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/2014slx/p/9407232.html

时间: 2024-08-01 20:50:12

hdu多校第4场 B Harvest of Apples(莫队)的相关文章

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)是可以在很快的时间下转移

2014 HDU多校弟八场H题 【找规律把】

看了解题报告,发现看不懂 QAQ 比较简单的解释是这样的: 可以先暴力下达标,然后会发现当前数 和 上一个数 的差值是一个 固定值, 而且等于当前数与i(第i个数)的商, 于是没有规律的部分暴力解决,有规律的套公式 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring&g

2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题

听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到100即可 算最大GPA的时候,首先每个科目分配到60分,然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到85即可,如果还有REMAIN POINT,就FOR循环下来加到100上限即可 不会DP 阿 QAQ 过段时间得好好看DP了  =  = 于是默默的把这题标记为<水题集> //

2014 HDU多校弟五场J题 【矩阵乘积】

题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存开辟都开不出来 1 #pragma comment(linker, "/STACK:16777216") 2 #include <iostream> 3 #include <stdio.h> 4 #define ll long long 5 using namesp

2014 HDU多校弟五场A题 【归并排序求逆序对】

这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的方法,可以拿来当模板 TVT 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream&g

2014 HDU多校弟六场J题 【模拟斗地主】

这是一道5Y的题目 有坑的地方我已在代码中注释好了 QAQ Ps:模拟题还是练的太少了,速度不够快诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <climits> #include <cstring> #include <cmath> #inclu

2018 HDU多校第三场赛后补题

2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube 题意: 在画布上画一个三维立方体. 题解: 模拟即可. 代码: #include <bits/stdc++.h> using namespace std; int a, b, c, R, C; char g[505][505]; int main () { int T; cin >>

hdu多校第3场C. Dynamic Graph Matching

Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1215 Accepted Submission(s): 505 Problem Description In the mathematical discipline of graph theory, a matching

hdu多校第三场 1006 (hdu6608) Fansblog Miller-Rabin素性检测

题意: 给你一个1e9-1e14的质数P,让你找出这个质数的前一个质数Q,然后计算Q!mod P 题解: 1e9的数据范围pass掉一切素数筛法,考虑Miller-Rabin算法. 米勒拉宾算法是一种判断素数的随机化算法,由于其随机性,它不能保证总是正确的,但其对于一个素数,总会返回素数的结果,对于一个合数,才有极小概率返回素数的结果(假阳性). 米勒拉宾算法对于单个素数的判断时间复杂度为$O(log^3n)$.(因为1e14相乘会爆longlong,模乘要写成龟速乘,因此要多一个log) 18