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 query&a,const query&b){
    return a.n<b.n;
}
int reflect[N];///分块
vector<query>lis[2000];
int fac[N],inv[N];
int quick(int a,int b){
    int odd = 1;
    while (b){
        if(b&1)odd = 1ll*odd*a%mod;
        a = 1ll*a*a%mod;
        b>>=1;
    }
    return odd;
}
const int mx = 100000;
void deal(){
    fac[0] = 1;
    for(int i = 1 ; i <= mx ; ++i){
        fac[i] = 1ll*i*fac[i-1]%mod;
    }
    inv[mx] = quick(fac[mx],mod-2);
    for(int i = mx ; i ; --i){
        inv[i-1] = 1ll*i*inv[i]%mod;
    }
}
int res[N];
int C(int n,int m){
    return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int main() {
    deal();
    int cent = sqrt(mx);
    int cnt = 1;
    for (int i = 1; i <= mx; i += cent, ++cnt) {
        for (int j = i; j <= mx and j <= i + cent; ++j) {
            reflect[j] = cnt;
        }
    }
    int T;
    scanf("%d",&T);
    for(int i = 1 ; i <= T ; ++i){
        scanf("%d %d",&Q[i].n,&Q[i].k);
        Q[i].i = i;
        lis[reflect[Q[i].k]].push_back(Q[i]);
    }
    for(int i = 1 ; i <= cnt ; ++i)if(!lis[i].empty()){
        sort(lis[i].begin(),lis[i].end(),cmp);
        int val = 0,in = lis[i][0].n,ik = -1;
        for(auto &j : lis[i]){
            while (in<j.n)val = (0ll + val + val + mod - C(in++,ik))%mod;
            while (ik<j.k)val = (val + C(in,++ik))%mod;
            while (ik>j.k)val = (val + mod - C(in,ik--))%mod;
            res[j.i] = val;
        }
    }
    for(int i = 1 ; i <= T ; ++i){
        printf("%d\n",res[i]);
    }
}

标程太秀了,顶礼膜拜。

原文地址:https://www.cnblogs.com/DevilInChina/p/9403578.html

时间: 2024-10-07 18:49:07

HDU 6311 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 5381 The sum of gcd 莫队+预处理

The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj=igcd(ai,ai+1....aj) Input There are multiple test cases. The first line

HDU 5145 NPY and girls(莫队算法+乘法逆元)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解] 我们发现每次往里加入一个新的数字或者减去一个新的数字,前后的排列数目是可以通过乘除转移的,所以自然想到用莫队算法处理.因为答案要求取模,所以在用除法的时候要计算逆元. [代码] #include <cstdio> #include <algorithm> #include <

HDU 5145 NPY and girls (莫队分块离线)

题目地址:HDU 5145 莫队真的好神奇..这样的复杂度居然只有n*sqrt(n)... 裸的莫队分块,先离线,然后按左端点分块,按块数作为第一关键字排序,然后按r值作为第二关键字进行排序.都是从小到大,可以证明这样的复杂度只有n*sqrt(n).然后进行块之间的转移. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <

HDU 5381 The sum of gcd 莫队暴力

链接 题解链接:http://www.cygmasot.com/index.php/2015/08/15/hdu_5381/ 题意: 给定n长的序列 下面n个数给出这个序列 m个询问 下面m行给出询问的区间. 对于一个询问,输出这个区间内的任意子段的gcd 和. 思路: 因为一个数的gcd只会不变或下降,下降一次至少减半,下降至多32次,所以处理出每个数连续相同的gcd的区间. 然后暴力跑莫队. #pragma comment(linker, "/STACK:1024000000,1024000

HDU 4638 Group (莫队算法||线段树离散查询)

题目地址:HDU 4638 先写了一发莫队,莫队可以水过.很简单的莫队,不多说. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <s

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

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 5145】 NPY and girls(组合+莫队)

pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 593    Accepted Submission(s): 179 Problem Description NPY's girlfriend blew him out!H