题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333

题目:

题意:求C(n,0)+C(n,1)+……+C(n,m)的值。

思路:由于t和n数值范围太大,所以此题查询复杂度不能太高,由组合数的将前k项求和可以推出,从而可以转换成莫队的区间查询,将n当成l,m当成r即可。此题需要注意,对于求组合数得用o(1)的方法求,也就是阶乘相除的方法,对于分母我们得求逆元,因而借助欧拉定理。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int t, block;
ll sum;
ll a[maxn], b[maxn]; struct node {
int l, r, id;
ll ans;
bool operator < (const node & x) const {
return (l - ) / block == (x.l - ) / block ? r < x.r : l < x.l;
}
}ask[maxn]; ll Mod_Pow(ll x, ll n) {
ll res = ;
while(n > ) {
if(n & ) res = res * x % mod;
x = x * x % mod;
n >>= ;
}
return res;
} void init() {
a[] = ;
for(int i = ; i < maxn; i++) a[i] = a[i-] * i % mod;
for(int i = ; i < maxn; i++) b[i] = Mod_Pow(a[i], mod - );
} ll C(int n, int m) {
if(n < || m < || m > n) return ;
if(m == || m == n) return ;
return a[n] * b[n-m] % mod * b[m] % mod;
} int main() {
//FIN;
init();
scanf("%d", &t);
block = sqrt(maxn);
sum = ;
for(int i = ; i <= t; i++) {
scanf("%d%d", &ask[i].l, &ask[i].r);
ask[i].id = i;
}
sort(ask + , ask + t + );
for(int i = , l = , r = ; i <= t; i++) {
while(l < ask[i].l) sum = ( * sum - C(l++, r) + mod) % mod;
while(l > ask[i].l) sum = ((sum + C(--l, r)) * b[]) % mod;
while(r < ask[i].r) sum = (sum + C(l, ++r)) % mod;
while(r > ask[i].r) sum = (sum - C(l, r--) + mod) % mod;
ask[ask[i].id].ans = sum;
}
for(int i = ; i <= t; i++) {
printf("%lld\n", ask[i].ans);
}
return ;
}

Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)的更多相关文章

  1. Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...

  2. 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

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

  4. HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))

    2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

  5. 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)    设 ...

  6. Problem B. Harvest of Apples 莫队求组合数前缀和

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

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

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. Problem L. Visual Cube(杭电多校2018年第三场+模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...

随机推荐

  1. week1 四人小组项目

    小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 项目选题:东北师范大学论坛 作为东北师范大学同学间的信息交流平台,要满足的需求如下: 1.校内信息及公告 2.毕业生招聘信息 3.课程查 ...

  2. C/C++打印堆栈信息

    转自:http://www.cnblogs.com/zhurizhe/p/3412369.html 在C/C++程序中打印当前函数调用栈 前几天帮同事跟踪的一个程序莫名退出,没有core dump(当 ...

  3. 利用Fiddler,解密wireshark抓的HTTPS包

    背景介绍 HTTPS加密方式介绍 浏览器-->SSL Client Hello(我支持这些加密方式)-->服务器 浏览器<-SLL Server Hello(就用这种加密,然后下面是 ...

  4. SpringBoot2.0(四) 远程调试

    和tomcat远程调试近似的配置,主要的配置如下所示: -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=10009 在 ...

  5. lambda 分组练习

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<Person> per ...

  6. matlab exist函数

    函数作用:用于确定某变量或值是否存在. 调用格式: exist主要有两种形式,一个参数和两个参数的,作用都是用于确定某值是否存在:1. b = exist( a) 若 a 存在,则 b = 1: 否则 ...

  7. RT-thread内核之线程调度算法

    一个操作系统如果只是具备了高优先级任务能够“立即”获得处理器并得到执行的特点,那么它仍然不算是实时操作系统.因为这个查找最高优先级线程的过程决定了调度时间是否具有确定性,例如一个包含n个就绪任务的系统 ...

  8. python函数入门到高级

    函数的定义: def test(x): "The function definitions" x+=1 return x def:定义函数的关键字 test:函数名 ():内可定义 ...

  9. 整合常用功能的JS小组件库-v1.0

    function Alex() { //给予video.js的页面滚动到视频元素范围内自动播放/出范围暂停播放-----01 this.video_autoplay = function (box) ...

  10. main方法为什么是静态的

    main函数其实也是所在类的一个方法,就比如一个类是test,那么该类的main其实就是test.main(String[] args),众所周知如果一个方法不是静态的,则要先实例化该类,比如要这样  ...