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 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

解析  不难发现S(n,m)也满足左上角加右上角(杨辉三角)  所以根据公式可以O(1)得到S(n-1,m),S(n+1,m),S(n,m-1),S(n,m+1) 可以看做区间的转移 从而套用莫队实现求解

AC代码

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan prllf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
typedef long long ll;
const ll maxn=1e5+,inf=0x3f3f3f3f;
const ll mod=1e9+;
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll fac[maxn],inv[maxn],ans[maxn];
ll chunk;
struct node
{
ll l,r,id,chunk;
}q[maxn];
bool cmp(node a,node b)
{
if(a.chunk!=b.chunk)
return a.l<b.l;
return a.r<b.r;
}
void init()
{
fac[]=fac[]=;
inv[]=inv[]=;
for(ll i=;i<maxn;i++)
{
fac[i]=fac[i-]*i%mod;
inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
}
for(ll i=;i<maxn;i++) //不可以写成一个for inv还会用到
inv[i]=inv[i-]*inv[i]%mod; //可以再开一个数组 写成一个for
}
ll C(ll x,ll y)
{
if(y>x) return ;
return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
init();//预处理组合数逆元 从而O(1)获得组合数 实现转移
ll t;
chunk=sqrt(maxn);
scanf("%lld",&t);
for(ll i=;i<=t;i++)
{
ll n,m;
scanf("%lld%lld",&n,&m);
q[i]=node{n,m,i,n/chunk+};
}
sort(q+,q++t,cmp);
ll l=,r=,res=;
for(ll i=;i<=t;i++)
{
while(l<q[i].l)
{
res=(res*%mod-C(l,r)+mod)%mod;
l++;
}
while(l>q[i].l)
{
l--;
res=(res+C(l,r))%mod*inv[]%mod;
}
while(r>q[i].r)
{
res=(res-C(l,r)+mod)%mod;
r--;
}
while(r<q[i].r)
{
r++;
res=(res+C(l,r))%mod;
}
ans[q[i].id]=res;
}
for(ll i=;i<=t;i++)
printf("%lld\n",ans[i]);
return ;
}

HDU 6333 莫队+组合数的更多相关文章

  1. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  2. Hdu 5213-Lucky 莫队,容斥原理,分块

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others)    Me ...

  3. HDU6333 莫队+组合数

    题目大意: 给定n m 在n个数中最多选择m个的所有方案 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3 ...

  4. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  5. HDU 4638 (莫队)

    题目链接:Problem - 4638 做了两天莫队和分块,留个模板吧. 当插入r的时候,设arr[r]代表r的位置的数字,判断vis[arr[r-1]]和vis[arr[r+1]]是否访问过,如果两 ...

  6. HDU 4638 莫队算法

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. 联赛模拟测试12 C. sum 莫队+组合数

    题目描述 分析 \(80\) 分的暴力都打出来了还是没有想到莫队 首先对于 \(s[n][m]\) 我们可以很快地由它推到 \(s[n][m+1]\) 和 \(s[n][m-1]\) 即 \(s[n] ...

  9. HDU 6534 莫队+ 树状数组

    题意及思路:https://blog.csdn.net/tianyizhicheng/article/details/90369491 代码: #include <bits/stdc++.h&g ...

随机推荐

  1. JAVA之NIO按行读取大文件

    做项目过程中遇到要解析100多M的TXT文件,并入库.用之前的FileInputStream.BufferedReader显然不行了,虽然readLine这方法可以直接按行读取,但是去读一个140M左 ...

  2. tac命令

    tac——显示文件内容(反列显示) 命令所在路径:/usr/bin/tac 示例1: # tac /etc/hosts 反列显示/etc/目录下hosts文件内容 ☛适合查看内容较短的文件

  3. Windows程序设计2(消息机制、菜单)

    一 .小记; PostQuitMessage(0); 产生WM_QUIT消息给进程队列,且立即返回,同时使得消息循环退出,使得进程终止.(其实它通过PostMessage(hWnd,WM_QUIT,0 ...

  4. vue 模块 props

    inbody.vue <template> <div> <Breadcrumb :style="{margin: '24px 0'}"> < ...

  5. js模块化入门与commonjs解析与应用

    JS模块化的基本原理 commonjs规范 commonjs在前端模块化中的基本使用 AMD与CMD规范剖析博客链接 一.JS模块化基本原理 在JS没有提出来模块化的时候,开发JS项目比较简单,同时也 ...

  6. tensorflow note

    #!/usr/bin/python # -*- coding: UTF- -*- # @date: // : # @name: first_tf_1223 # @author:vickey-wu fr ...

  7. node程序的部署神器pm2的基本使用

    pm2是从nodejs衍生出来的服务器进程管理工具,可以做到开机就启动nodejs.当然了,也可以用nohup来做这件事情的. 前言 众所周知,Node.js运行在Chrome的JavaScript运 ...

  8. 10MongoDB

    一.  介绍MongoDB 1. NoSQL 1)“NoSQL”⼀词最早于1998年被⽤于⼀个轻量级的关系数据库的名字 随着web2.0的快速发展, NoSQL概念在2009年被提了出来 2)NoSQ ...

  9. C#语言之字符串和正则表达式

    本文将完成以下两个目标: 一.创建字符串: 二.正则表达式: 首先,我先来介绍一下System.String类: System.String是一个类,专门用于存储字符串,允许对字符串进行许多操作. 使 ...

  10. 【HDU 2028】Lowest Common Multiple Plus

    Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数 ...