hdu6333 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): 3088 Accepted Submission(s): 1201
Count the number of ways to pick at most m apples.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
5 2
1000 500
924129523
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll block, a[maxn], b[maxn];
struct node {
ll le, ri, id, ans;
};
node ask[maxn];
bool cmp( node p, node q ) {
if( (p.le-1)/block == (q.le-1)/block ) { //分成block大小的几块,看n属于哪一块,每一块里面按m排序
return p.ri < q.ri;
} else {
return p.le < q.le;
}
}
ll qow( ll a, ll b ) { //快速幂用于求逆元
ll ans = 1;
while(b) {
if( b&1 ) {
ans = ans*a%mod;
}
a = a*a%mod;
b /= 2;
}
return ans;
}
void init() {
a[1] = 1;
for( ll i = 2; i < maxn; i ++ ) { //计算i的阶乘
a[i] = a[i-1]*i%mod;
}
for( ll i = 1; i < maxn; i ++ ) { //计算i的阶乘的逆元
b[i] = qow(a[i],mod-2);
}
}
ll C( ll n, ll m ) { //计算组合数C(n,m)
if( n < 0 || m < 0 || m > n ) {
return 0;
}
if( m == 0 || m == n ) {
return 1;
}
return (a[n]*b[n-m]%mod)*b[m]%mod;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
init();
ll T, sum = 1;
block = sqrt(maxn); //以sqrt(maxn)大小分成几块
scanf("%lld",&T);
for( ll i = 1; i <= T; i ++ ) { //离线查询
scanf("%lld%lld",&ask[i].le,&ask[i].ri);
ask[i].id = i;
}
sort(ask+1,ask+T+1,cmp); //按照cmp排序从小到大可以线性计算结果,节省时间
for( ll i = 1, le = 1, ri = 0; i <= T; i ++ ) {
while( le < ask[i].le ) { //S(n,m)=2S(n-1,m)-C(n-1,m)
sum = (2*sum-C(le++,ri)+mod)%mod;
}
while( le > ask[i].le ) { //S(n,m)=(S(n+1,m)+C(n,m))/2
sum = ((sum+C(--le,ri))*b[2])%mod;
}
while( ri < ask[i].ri ) { //S(n,m)=S(n,m-1)+C(n,m)
sum = (sum+C(le,++ri))%mod;
}
while( ri > ask[i].ri ) { //S(n,m)=S(n,m+1)-C(n,m)
sum = (sum-C(le,ri--)+mod)%mod;
}
ask[ask[i].id].ans = sum; //按标号顺序存放结果,只利用了ans,这样不会对后面有的计算造成影响
}
for( ll i = 1; i <= T; i ++ ) {
printf("%lld\n",ask[i].ans);
}
return 0;
}
hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)的更多相关文章
- HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...
- HDU6333 Harvest of Apples (杭电多校4B)
这莫队太强啦 先推公式S(n,m)表示从C(n, 0) 到 C(n, m)的总和 1.S(n, m) = S(n, m-1) + C(n, m) 这个直接可以转移得到 2.S(n, m) = ...
- hdu6333 Problem B. Harvest of Apples(组合数+莫队)
hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m) 设 ...
- HDU 6333 Harvest of Apples (分块、数论)
题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...
- cf666 C. Codeword 组合数学 离线分块思想
time limit per test 6 seconds memory limit per test 256 megabytes input standard i ...
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 2018年多校第四场第二题 B. Harvest of Apples hdu6333
题意:给定10^5以内的n,m求∑组合数(n,i),共10^5组数据. 题解: 定义 S(n, m) = \sum_{i = 0} ^ {m} {n \choose i}S(n,m)=∑i=0m ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
- HDU6333 求组合数前m项的和
目录 分块 莫队 @ HDU6333:传送门 题意:求组合数前m项的和. 在线分块or离线莫队 分块 重要的一个定理: \[C_{n}^{m} = 0\;\;m > n\] \[C_{n}^{m ...
随机推荐
- Is it a full physical image???
My friend asked me why she could not find some important files in a physical image acquired from an ...
- 【Java例题】7.1 线程题1-时间显示线程
1.时间显示线程.设计一个示线程子类,每秒钟显示一次当前时间:然后编写主类,在主函数中定义一个线程对象,并启动这个线程 package chapter7; import java.text.Simpl ...
- windows环境composer install失败的解决办法
报错信息:[Composer\Downloader\TransportException] The "https://repo.packagist.org/p/doctrine/inflec ...
- Linux--shell重定向与文件处理命令--02
一.IO重定向 1.数据输入:键盘---标准输入,但并不是唯一输入方式 ” | passwd –stdin username #同时添加用户和密码 while line;do 循环体...$line ...
- windows server2012 nVME和网卡等驱动和不识别RAID10问题
安装2012---不识别M.2 nVME,下官方驱动,注入到系统里 缺多驱动---用ITSK万能驱动添加:|Win8012R2.x64(可解决不支持操作系统,win10与server2012R2通用) ...
- kvm 内部错误:无法找到适合 x86_64 的模拟器
0x00 问题 安装完 KVM 之后,启动管理工具报错:内部错误:无法找到适合 x86_64 的模拟器 于是查看 libvirtd 服务状态,查看到以下内容: 6月 14 10:18:53 local ...
- Apex 获取真正的IP地址
代码如下 declare l_ip varchar2(15); begin if OWA_UTIL.GET_CGI_ENV('X-FORWARDED-FOR') is not null then l_ ...
- Go输入输出格式化Printf
package main import ( "fmt" "os" ) type point struct { x, y int } func main() { ...
- spring-boot-plus快速开始 Quick Start(一)
spring-boot-plus快速开始 Quick Start 1. clone项目到本地 shell script git clone git@github.com:geekidea/spring ...
- NOIP前的模板复习和注意事项
联赛除去今天刚好只有一个星期了,最后一个星期也很关键,要吃好睡好保持心情愉悦.当然也免不了最后的复习计划. 首先是模板,之前还有很多模板没有复习到,这些东西是一定要落实到位的. 每天往后面写一点... ...