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 ...
随机推荐
- CSS3自定义字体
原文摘自:https://www.cnblogs.com/moqiutao/archive/2015/12/23/5070463.html 总节: 1) 定义字体标准格式: @font-face { ...
- react项目总结
1.基本框架 1.react+react-router4+redux3.7.2 2.css预编译使用sass 3.数据请求使用axios(原本是使用fetch,结果在ios10下报错) 4.ui组件库 ...
- python中的迭代器与生成器
迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...
- js如何判断客户端是iOS还是Android等移动终端
判断原理:JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来 ...
- 1 wait notify
wait/notify: wait()使线程停止,notify使wait状态的线程继续执行. wait()是Object类的方法,该方法用来将线程置入“预执行队列”,并在wait()方法处停止执行,直 ...
- CPU设计学习-流水线
各种名词 标量流水线 超级流水线 超标量流水线与多发射技术 经典五级流水线 IF |Instruction Fetch,取指 ID |Instruction Decode,译码 EX |Execute ...
- day-17 L1和L2正则化的tensorflow示例
机器学习中几乎都可以看到损失函数后面会添加一个额外项,常用的额外项一般有两种,一般英文称作ℓ1-norm和ℓ2-norm,中文称作L1正则化和L2正则化,或者L1范数和L2范数.L2范数也被称为权重衰 ...
- HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...
- Java常用类之Math类
Java 的常用类Math类: java.lang.Math 提供了系列的静态方法用于科学计算,其方法的参数和返回值类型一般为 double 类型. 如: 1. public static final ...
- ubuntu 安装lua错误
转自:http://www.cnblogs.com/softidea/archive/2016/03/02/5236498.html lua.c:80:31: fatal error: readlin ...