Problem B. Harvest of Apples 莫队求组合数前缀和
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 <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
int t, sz;
LL inv[maxn], a[maxn], b[maxn];
struct node {
int l, r, id;
LL ans = ;
} qu[maxn];
int cmp(node a, node b) {
return a.l / sz == b.l / sz ? a.r < b.r : a.l < b.l;
}
LL expmod(LL a, LL b) {
LL ans = ;
while(b) {
if (b & ) ans = ans * a % mod;
a = a * a % mod;
b = b >> ;
}
return ans;
}
void init() {
a[] = ;
for (int i = ; i < maxn ; i++) a[i] = a[i - ] * i % mod;
for (int i = ; i < maxn ; i++) b[i] = expmod(a[i], mod - );
}
LL C(int n, int m) {
if (m > n || n < || m < ) return ;
if (m == n || m == ) return ;
return a[n] * b[m] % mod * b[n - m] % mod;
} int main() {
init();
sf(t);
for (int i = ; i <= t ; i++) {
sff(qu[i].l, qu[i].r);
qu[i].id = i, qu[i].ans = ;
}
sz = sqrt(maxn);
sort(qu + , qu + + t, cmp);
LL sum = ;
for (int i = , L = , R = ; i <= t ; i++) {
while(L < qu[i].l) sum = ( * sum - C(L++, R) + mod) % mod;
while(L > qu[i].l) sum = ((sum + C(--L, R)) * b[]) % mod;
while(R < qu[i].r) sum = (sum + C(L, ++R)) % mod;
while(R > qu[i].r) sum = (sum - C(L, R--) + mod) % mod;
qu[qu[i].id].ans = sum;
}
for (int i = ; i <= t ; i++) printf("%lld\n", qu[i].ans);
return ;
}
Problem B. Harvest of Apples 莫队求组合数前缀和的更多相关文章
- HDU-6333 Problem B. Harvest of Apples 莫队
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...
- 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)).有公 ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- 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) 设 ...
- Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...
- HDU - 6333 Problem B. Harvest of Apples (莫队)
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- 热身训练1 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 题意: 求 C(0,n)+C(1,n)+...+C(m,n) 分析: 这道题,我们令s(m,n) = C( ...
- codeforce617E-XOR and Favorite Number莫队+异或前缀和
传送门:http://codeforces.com/contest/617/problem/E 参考:https://blog.csdn.net/keyboarderqq/article/detail ...
随机推荐
- PNG和PVR之间互相转换的脚本
项目经常会将png和pvr之间互相转换,这里mark一个脚本,会将当前目录下的文件全部批量转换 png转换成pvr @echo off path %path%;"C:\Program Fil ...
- Memcache的客户端连接系列(四) PHP
关键词: Memcached PHP 客户端 声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(Memcached)的用户指南.客户端连接方法通用,故摘抄过来分享给大家. PHP客户端 Re ...
- C++ ifndef /define/ endif 作用和用法
ifndef/define/endif”主要目的是防止头文件的重复包含和编译 比如你有两个C文件,这两个C文件都include了同一个头文件.而编译时,这两个C文件要一同编译成一个可运行文件,于是问题 ...
- 最小生成树(II)与Kruskal算法
为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...
- jQuery 调用后台方法(net)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...
- POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)
Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...
- [mongodb]child process failed, exited with error number 100
Run the following command first to start the mongo server mongod run --config /usr/local/etc/mongod. ...
- <Android>对话框的使用
Android系统提供四种对话框:警告对话框(AlertDialog),进度对话框(ProgressDialog),日期选择对话框(DatePickerDialog)和时间选择对话框(TimePick ...
- 【week3】四人小组项目—东师论坛
项目选题:东北师范大学论坛 小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳. 本周任务: 1.发布申请 功能列表: 1.注册,登录 2.校内信息公告推送 3.十大热点 (根据搜索量.评 ...
- 移除 ios 上 input 的默认样式
input{ -webkit-appearance:none; }