HDU_1175_莫队+逆元】的更多相关文章

http://acm.hdu.edu.cn/showproblem.php?pid=5145 初探莫队,就是离线排序后的暴力,但是效率大大提高,中间要除法取模,所以用到了逆元. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define LL long long #define MOD 1000000007 us…
NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more!However, he has so many girlfriend candidates.Because there are too many girls and for the convenience of management, NPY numbered the girls from 1 to…
链接:https://ac.nowcoder.com/acm/contest/992/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述   n个不同的滑稽果中,每个滑稽果可取可不取,从所有方案数中选取一种,求选取的方案中滑稽果个数不超过m的概率.(对109+7取模)输入描述:第一行一个正整数T( T <= 10^5 )随后T行每行两个整数n,m ( 0 < m <= n…
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解] 我们发现每次往里加入一个新的数字或者减去一个新的数字,前后的排列数目是可以通过乘除转移的,所以自然想到用莫队算法处理.因为答案要求取模,所以在用除法的时候要计算逆元. [代码] #include <cstdio> #include <algorithm> #include <…
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 4043    Accepted Submission(s): 1560 Problem Description There a…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = Cmn-1 + Cm-1n-1  这个公式知道杨辉三角的都明白,但是一看发现似乎没啥用.但是可以以这个公式为基础继续推演下去. 设Snm = Cn1 + Cn2 + Cn3 + ...... Cnm 然后继续使用上面的基本公式可以化成 Sn m-1 = Sn m - Cn m Sn m+1 = Sn…
题目链接: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 &…
2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨一些东西. 这题肯定不能一个一个遍历求和,这样就上天了... 解释一下官方题解的意思. 为什么 sum(n,m)=2*sum(n-1,m)-c(n-1,m). 因为c(n,m)=c(n-1,m)+c(n-1,m-1),至于为什么成立,不懂的百度一下组合数和杨辉三角吧... sum(n,m)=c(n,…
NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 790    Accepted Submission(s): 280 Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more!However, he…
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到奶奶都不认识了.当时想了好多好多,各种骚操作都想了一遍就是没想到居然是莫队....我用S(n,m)来记录C(n,0)+...+C(n,m)的和作为一个询问的答案 由组合数公式C(n,m) = C(n-1,m-1)+C(n-1,m)可以推的下面的式子 S(n,m) = S(n,m-1) + C(n,m…
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一个数,\(区间值*区间长度/该数出现次数\),减去一个数则相反操作讲解 trick 1.我的原先莫队写法不能ac,原因是我传入的L,R是全局变量,在insert和erase前就++,--了,而没有达到预期目的,把R++,L--分离即可 代码 //wa #include <bits/stdc++.h>…
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more!However, he has so many girlfriend candidates.Because there are too many girls and…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下面有m个询问,每个询问有l,r两个数,表示要去找编号为l到r的女孩,每进一次教室只能找一个女孩,问有多少种组合方式,不同的组合方式定义为去教室的顺序不同. 题解:这题是离线的,很容易想到用莫队分块做,主要是要找出状态转移的方程,对于询问区间[l,r],假设这个区间的女孩一共分布在k个教室,每个教室有…
Description 现在有一个长度为\(~n~\)的数列\(~A_1~,~A_2~\dots~A_n~\),\(~Q~\)个询问\(~[l_i~,~r_i]~\),每次询问区间内是否有元素相同 Input 第一行有两个整数\(~N,Q~\), 第二行有\(~n~\)个整数,代表这个序列 以下\(~Q~\)行每行两个整数,代表询问区间 Output 对每个询问输出一行\(~Yes~\)或\(~No~\). Hint \(Forall:\) \(1~\leq~n~,~Q~\leq~10^5~,~…
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…
题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公式:S(n,m) = S(n,m-1) +C(n,m)以及S(n,m) = 2*S(n-1,m) - C(n-1,m). 这样就可以在O(1)的时间中计算出S(n+1,m),S(n-1,m),S(n,m+1),S(n,m+1).可以用莫队离线处理T组查询. #include<bits/stdc++.h&…
题目大意: 给定n m 在n个数中最多选择m个的所有方案 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LL long long ; ; /********组合数模板*********/ LL pow_mod(LL a, LL b) { LL res = 1LL; a %= mod; while(b){ ) res = res * a % mod; a = a * a % mod; b…
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 th…
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 593    Accepted Submission(s): 179 Problem Description NPY's girlfriend blew him out!H…
HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的方法总数,显然是C(n,0),C(n,1)……C(n,m)的和. 显然S(n,m+1) = S(n, m) + C(n,m+1); 还有一个等式就不那么明显了,S(n+1,m) = 2 * S(n,m) - C(n,m); 我也是在王神犇的指导下明白的. 既然知道了一组(n,m)是可以在很快的时间下…
题目链接:洛谷 这个跟上上个Ynoi题目是一样的套路,首先我们知道\(n=\prod p_i^{\alpha_i}\)时\(d(n)=\prod (\alpha_i+1)\). 首先对所有数分解质因数,首先预处理\(\leq \sqrt{\max a_i}\)的所有质数,然后一个一个试除,时间复杂度\(O(\frac{n\sqrt{a_i}}{\log{a_i}})\),在lxl的数据下跑得飞快(大家都知道,卡常是要看数据性质的).或者使用Pollard-rho分解也是可以的. 然后莫队,维护\…
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4542 题解 我们令 \(f_i\) 表示从 \(i\) 到 \(n\) 位组成的数 \(\bmod P\) 的值. 那么一个从 \(l, r\) 的串的权值为 \(\frac{f_l - f_{r+1}}{10^{n-r}}\). 如果需要这个东西 \(=0\),也就是 \[ \frac{f_l - f_{r+1}}{10^{n-r}} = 0 \pmod P \] 下一步显然是要把 \(1…
hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m)    设 S(n,m)=C(n,0)+C(n,1)+C(n,2)+...+C(n,m) 然后将S(n,m) 通过 第一个公式 拆项 最后化简 变为 S(n,m)=2*S(n-1,m)-C(n-1,m); 即: 所以可以离线用莫队算法 参考博客:链接1.链接2 代码: #include <bits/stdc+…
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm apples.  Input The first line of the input contains an integer TT (1≤T≤105)(1≤T≤105) denoting the number of test cases. Each test case consists of one…
洛谷题面传送门 一道其实算得上常规的题,写这篇题解是为了总结一些数论中轻微(?)优化复杂度的技巧. 首先感性理解可以发现该问题强于区间数颜色问题,无法用常用的 log 数据结构维护,因此考虑分块/莫队.显然这题莫队比较好些对吧?显然我们要对每个质因子计算一遍它在 \([l,r]\) 中的出现次数对吧?涉及质因子就要分解质因数对吧?莫队时候新添一个元素很明显就要枚举它的每个质因子,然后计算新添的贡献对吧?线性预处理乘法逆元以后,复杂度就变成了 \(n\sqrt{a_i}+(n+q)\sqrt{n}…
3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Discuss] Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的,只能用Mato自己写的程序才能访问.Mato每天随机选一个区间[l,r],他今天就看编号在此区间内的这些资料.Mat…
Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 1457 Appoint description:  Description Sona, Maven of the Strings. Of cause, she can play the zither. Sona can't speak but she can make fancy music. Her…
学莫队必做题,,,但是懒得写.今天来填个坑 莫队水题 莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学) 易错点:要开long long(卡我半天) #include <cstdio> #include <cmath> #include <algorithm> using namespace std; ],a[]; ],an[]; ]; bool operator<(que a,que b){return ((…
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Submit][Status][Discuss] Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只…
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: standard input output: standard output Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pa…